[英]WPF SVG image binding using a converter
I would like to change my image source from:我想从以下位置更改我的图像源:
<Image Source="{svg:SvgImage image.svg}"/>
To something that use binding on an enum property instead:对于在枚举属性上使用绑定的东西:
XAML:
XAML:
<Resources>
<local:MyConverter x:Key="MyConverter" />
</Resources>
<Image Source="{svg:SvgImage Binding MyEnumProperty, Converter={StaticResource MyConverter}}" />
Code behind:
后面的代码:
public enum MyEnum
{
Value1,
Value2
}
public class MyConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var myValue = (MyEnum)(value);
switch (myValue)
{
case MyEnum.Value1:
return "image1.svg";
case MyEnum.Value2:
return "image2.svg";
default:
throw new NotImplementedException();
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
This doesn't work and I suspect that is has something to do with the svg:SvgImage
and Binding MyEnumProperty
being combined in the same statement.这不起作用,我怀疑这与将
svg:SvgImage
和Binding MyEnumProperty
组合在同一语句中有关。
I get the following errors:我收到以下错误:
The member "Converter" is not recognized or is not accessible.
And和
The property 'Converter' was not found in type 'SvgImageExtension'.
Question: What is the correct way to do this?
问题:这样做的正确方法是什么?
The expression表达方式
{svg:SvgImage Binding MyEnumProperty ...}
is not valid XAML, and because SvgImage is a markup extension, you can't bind its properties.不是有效的 XAML,并且因为 SvgImage 是一个标记扩展,你不能绑定它的属性。
You may however use DataTriggers in an Image Style instead of a Binding with a Converter:但是,您可以在图像样式中使用 DataTriggers 而不是 Binding with a Converter:
<Image>
<Image.Style>
<Style TargetType="Image">
<Style.Triggers>
<DataTrigger Binding="{Binding MyEnumProperty}" Value="Value1">
<Setter Property="Source" Value="{svg:SvgImage image1.svg}"/>
</DataTrigger>
<DataTrigger Binding="{Binding MyEnumProperty}" Value="Value2">
<Setter Property="Source" Value="{svg:SvgImage image2.svg}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.