wpf - System.Byte[]' 不是属性 ImageSource 的有效值

标签 wpf mvvm data-binding

我有一个用户控件,我想在其中设置背景图像。如果我这样做,它会起作用:

风景:

<UserControl.Background>
        <ImageBrush AlignmentX="Center" AlignmentY="Center" Stretch="Uniform" Opacity="0.25" ImageSource="{Binding Logo}">
            <ImageBrush.RelativeTransform>
                <ScaleTransform ScaleX="0.75" ScaleY="0.75" CenterX=".5" CenterY="0.5" />
            </ImageBrush.RelativeTransform>
        </ImageBrush>
    </UserControl.Background>

View 模型中的属性:
private byte[] _logo = GlobalVariables.Logo;
        public byte[] Logo
        {
            get { return _logo; }
            set
            {
                _logo = value;
                base.RaisePropertyChangedEvent("Logo");
            }
        }

实际上,我在启动应用程序时将 Logo 加载到全局变量中,因为 Logo 存储在数据库中,这样我只需加载一次,而不是每次我想打印文档时加载。

所以它是一个全局变量,我试图直接从全局变量中获取 Logo ,这样我就可以避免在我的 View 模型中需要有一个属性。

所以在我看来,我正在尝试这段代码:
<UserControl.Background>
        <ImageBrush AlignmentX="Center" AlignmentY="Center" Stretch="Uniform" Opacity="0.25" ImageSource="{x:Static vg:GlobalVariables.Logo}">
            <ImageBrush.RelativeTransform>
                <ScaleTransform ScaleX="0.75" ScaleY="0.75" CenterX=".5" CenterY="0.5" />
            </ImageBrush.RelativeTransform>
        </ImageBrush>
    </UserControl.Background>

在这种情况下,我收到以下错误:

'System.Byte[]' no es un valor válido para la propiedad 'ImageSource'。

我不明白为什么,因为在有效的示例中,我的 View 模型中的属性也是 Byte[],实际上,在我的 View 模型中,我将 glbal 变量设置为属性。

谢谢。

最佳答案

使用静态属性 Binding 从内置类型转换(StaticExtension 显然没有使用)中受益:

<ImageBrush ImageSource="{Binding Path=(vg:GlobalVariables.Logo)}" .../>

请注意 Logo这里应该是一个公共(public)静态属性。

如果是静态字段,可以这样写Binding:
<ImageBrush ImageSource="{Binding Source={x:Static vg:GlobalVariables.Logo}}" .../>

关于wpf - System.Byte[]' 不是属性 ImageSource 的有效值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51947948/

相关文章:

c# - 我应该什么时候更新数据库以反射(reflect)属性变化?

c# - 我的逻辑在这里需要一点帮助。我怎样才能让图片在点击时选择更改图像?

wpf - 为什么在 Binding 中有效的格式字符串在 MultiBinding 中无效?

visual-studio-2010 - MvvmLight工具包安装程序4.0.23.1.msi,未安装VS2010模板

android - Webview 和 ProgressBar 的 2 种方式数据绑定(bind)

wpf - 在 ResourceDictionary 内的共享 WPF 资源中启用数据绑定(bind)

c# - 使用代码将图像对象添加到 wpf

c# - WPF:如何使 RichTextBox 看起来像 TextBlock?

wpf - 如何在 WPF/Silverlight 中设置页眉/行

android - StateFlow 在后退导航中再次获取相同的数据