[英]WPF Setting a Converter's public Image property from xaml
我有一個使用轉換器的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"/>
或多或少,翻譯說:
無法為OkImage的屬性分配值“ /My.Tools.Graphics;component/Images/Accept.png”。 類型“圖像”的“ OkImage”屬性不能指定為字符串。
My.Tools.Graphics是添加到我的Visual Studio解決方案中的DLL,其中包含一個名為Images的文件夾,其中包含png圖像。
不要將Image
(這是UIElement)用作Convert方法的返回類型。 而是使用ImageSource
,它(與Image相比)可以分配給Image的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();
}
}
作為綁定轉換器的替代方法,您還可以將圖像樣式與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>
您需要將圖像路徑分配給Image
對象的Source
屬性,而不是實際圖像(圖像路徑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"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.