c# - WPF 从 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"/>
或多或少,翻译它说:
无法将值“/My.Tools.Graphics;component/Images/Accept.png”分配给 OkImage 的属性。'Image' 类型的 '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>
推荐阅读
- excel - 需要一系列单元格的公式 - 根据另一个单元格范围中是否存在值来突出显示单元格
- node.js - Eslint plugin import throws "import/no-unresolved" on specific modules inside a Lerna package
- c# - 基于相同 ID 过滤数据
- php - Composer 更新:缺少请求的 PHP 扩展 ext-http
- reactjs - useEffect 中的 state 总是使用 React Hooks 引用初始状态
- c - 有没有办法在给定 /dev/input/eventX 文件描述符的情况下获取字符设备驱动程序的 struct input_dev ?
- ruby-on-rails - Rails 5 升级打破了延迟任务
- ruby-on-rails - Rails HTTParty 在 2 个不同的 API 中出现相同的 404 响应
- r - R:可以使用成像器加载多个图像以进行 lapply 操作吗?
- sql - 多个 CTE 查询,将单列值分配给局部变量