使用转换器的WPF SVG图像绑定

时间:2019-03-20 12:33:56

标签: c# wpf svg binding ivalueconverter

我想从以下位置更改图像来源:

insert into [ServerName1].DatabaseName.dbo.TableName(column1,column2)
select Column1, null from LocalTableName

对于在绑定属性上使用绑定的东西:

XAML:

<Image Source="{svg:SvgImage image.svg}"/>

背后的代码:

<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.

问题: 正确的方法是什么?

1 个答案:

答案 0 :(得分:1)

表达式

{svg:SvgImage Binding MyEnumProperty ...}

无效的XA​​ML,并且由于SvgImage是标记扩展,因此无法绑定其属性。

但是,您可以在图像样式中使用DataTriggers,而不是使用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>