WPF从xaml设置转换器的公共图像属性

时间:2018-06-04 10:59:42

标签: c# wpf .net-3.5 ivalueconverter

我有一个使用转换器的WPF应用程序:

public class MyResultImageConverter : IValueConverter  
{
    public Image OkImage { get; set; }
    public Image FailImage { get; set; }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null || !(value is MyCustomObject))
        {
            return null;
        }

        Image img = null;
        MyCustomObjectdbr = (MyCustomObject)value;
        switch (MyCustomObjectdbr.Code)
        {
            case (int)MyEnum.OK:
                img = this.OkImage;
                break;

            case (int)MyEnum.NOK:
                img  = this.FailImage;
                break;

            default:
                break;
        }

        return img;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

然后在窗口资源中我做:

<myConverters:MyResultImageConverter
     OkImage="/My.Tools.Graphics;component/Images/Accept.png" 
     FailImage="/My.Tools.Graphics;component/Images/Cancel.png"
     x:Key="MyResultImageConverter"/>

此转换器稍后在DataGridTemplateColumn中使用:

<dg:DataGridTemplateColumn 
                           Width="SizeToCells"
                           IsReadOnly="True">
    <dg:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Image Source="{Binding Path=MyResult, Converter={StaticResource MyResultImageConverter}}" />
        </DataTemplate>
    </dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>

尝试为转换器设置Image属性时,编译器会抛出错误:

<myConverters:MyResultImageConverter
     OkImage="/My.Tools.Graphics;component/Images/Accept.png" 
     FailImage="/My.Tools.Graphics;component/Images/Cancel.png"
     x:Key="MyResultImageConverter"/>

或多或少,翻译它说:

  

无法分配值&#34; /My.Tools.Graphics; component / Images / Accept.png&#34;   到OkImage&#39;属性。 &#39; OkImage&#39;属性&#39;图像&#39;不可能是   指定为字符串。

My.Tools.Graphics是一个添加到我的visual studio解决方案的DLL,其中包含一个名为Images的文件夹,其中包含png图像。

2 个答案:

答案 0 :(得分:3)

不要使用Image(这是一个UIElement)作为Convert方法的返回类型。相反,使用ImageSource,可以将(与图像相比)分配给图像的Source属性:

public class MyResultImageConverter : IValueConverter  
{
    public ImageSource OkImage { get; set; }
    public ImageSource FailImage { get; set; }

    public object Convert(
        object value, Type targetType, object parameter, CultureInfo culture)
    {
        var customObject = value as MyCustomObject;

        if (customObject == null)
        {
            return null;
        }

        return customObject.Code == MyEnum.OK ? OkImage : FailImage;
    }

    public object ConvertBack(
        object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

作为Binding Converter的替代方法,您还可以使用带有DataTrigger的图像样式:

<BitmapImage x:Key="OkImage" UriSource="/My.Tools.Graphics;component/Images/Accept.png"/>
<BitmapImage x:Key="FailImage" UriSource="/My.Tools.Graphics;component/Images/Cancel.png"/>
<Style x:Key="ResultImageStyle" TargetType="Image">
    <Setter Property="Source" Value="{StaticResource FailImage}"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding MyResult}" Value="OK">
            <Setter Property="Source" Value="{StaticResource OkImage}"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

...

<DataTemplate>
    <Image Style="{StaticResource ResultImageStyle}"/>
</DataTemplate>

答案 1 :(得分:-1)

您需要将图像路径分配给Source对象的Image属性,而不是实际图像(图像路径URI是String而不是Image )。

public class MyResultImageConverter : IValueConverter  
{
    public Image OkImage { get; set; }
    public Image FailImage { get; set; }

    public string OkImagePath { get; set{
         OkImage = new Image();
         OkImage.Source = value;
        }
    }

    public string FailImagePath { get; set{
         FailImage = new Image();
         FailImage.Source = value;
        }
    }

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    if (value == null || !(value is MyCustomObject))
    {
        return null;
    }

    Image img = null;
    MyCustomObjectdbr = (MyCustomObject)value;
    switch (MyCustomObjectdbr.Code)
    {
        case (int)MyEnum.OK:
            img = this.OkImage;
            break;

        case (int)MyEnum.NOK:
            img  = this.FailImage;
            break;

        default:
            break;
    }

    return img;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    throw new NotImplementedException();
}
}

<myConverters:MyResultImageConverter
     OkImagePath="/My.Tools.Graphics;component/Images/Accept.png" 
     FailImagePath="/My.Tools.Graphics;component/Images/Cancel.png"
     x:Key="MyResultImageConverter"/>