我有一个用户控件,我想在其中设置背景图像。如果我这样做,它会起作用:
风景:
<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/