使用转换器的 WPF SVG 图像绑定
WPF SVG image binding using a converter
我想更改图片来源:
<Image Source="{svg:SvgImage image.svg}"/>
改为对枚举使用绑定 属性:
XAML:
<Resources>
<local:MyConverter x:Key="MyConverter" />
</Resources>
<Image Source="{svg:SvgImage Binding MyEnumProperty, Converter={StaticResource MyConverter}}" />
后面的代码:
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();
}
}
这不起作用,我怀疑这与 svg:SvgImage
和 Binding MyEnumProperty
在同一语句中组合有关。
我收到以下错误:
The member "Converter" is not recognized or is not accessible.
和
The property 'Converter' was not found in type 'SvgImageExtension'.
问题:
正确的做法是什么?
表达式
{svg:SvgImage Binding MyEnumProperty ...}
无效XAML,并且由于 SvgImage 是标记扩展,您无法绑定其属性。
但是,您可以在图像样式中使用 DataTriggers 而不是使用转换器进行绑定:
<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>
我想更改图片来源:
<Image Source="{svg:SvgImage image.svg}"/>
改为对枚举使用绑定 属性:
XAML:
<Resources>
<local:MyConverter x:Key="MyConverter" />
</Resources>
<Image Source="{svg:SvgImage Binding MyEnumProperty, Converter={StaticResource MyConverter}}" />
后面的代码:
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();
}
}
这不起作用,我怀疑这与 svg:SvgImage
和 Binding MyEnumProperty
在同一语句中组合有关。
我收到以下错误:
The member "Converter" is not recognized or is not accessible.
和
The property 'Converter' was not found in type 'SvgImageExtension'.
问题: 正确的做法是什么?
表达式
{svg:SvgImage Binding MyEnumProperty ...}
无效XAML,并且由于 SvgImage 是标记扩展,您无法绑定其属性。
但是,您可以在图像样式中使用 DataTriggers 而不是使用转换器进行绑定:
<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>