如何根据绑定值更改 WPF 数据网格行的图像 [英] How to change the image of WPF datagrid row depending on binding value
问题描述
我是 WPF 的初学者.
I am a beginner to WPF .
我有一个数据网格,用于显示带有列定义的消息,如下所示.数据网格绑定到数据表
I have a data grid for showing messages with column definitions as below . Data grid is bound to a datatable
<my:DataGridTextColumn Binding="{Binding Module}" Header="Module"
Width="75" IsReadOnly="True"></my:DataGridTextColumn>
<my:DataGridTextColumn Binding="{Binding Record ID}" Header="RecordID"
Width="75" IsReadOnly="True"></my:DataGridTextColumn>
<my:DataGridTextColumn Binding="{Binding ItemName}"
Header="Item/Platform/Country Name" Width="175" IsReadOnly="True">
</my:DataGridTextColumn>
<my:DataGridTextColumn Binding="{Binding DateReceived}"
Header="DateReceived" Width="150" IsReadOnly="True">
</my:DataGridTextColumn>
<my:DataGridTextColumn Binding="{Binding Comments}" Header="Comments"
Width="300" IsReadOnly="True"></my:DataGridTextColumn>
现在我需要添加一个标题为 "Status" 的 coulmn .和内容作为图像.我绑定数据表的IsRead"列到此列,如果 IsRead 值为 False,我需要显示图像 unread.png,如果 IsRead 值为 True,我需要显示图像 read.png
Now I need to add a coulmn with header as "Status" . and content as image . I am binding "IsRead" column of the datatable to this column such that if the IsRead value is False i need to show image unread.png and if the IsRead value is True i need to show image read.png
我该怎么做?
推荐答案
您可以在包含绑定属性的类中创建一个 StatusImage 属性:
You could create a StatusImage property in the class that holds your binding properties:
public string StatusImage {
get
{
if (IsRead)
return "read.png";
return "unread.png";
}
}
然后将其绑定到图像例如:
And then bind it to the image for example:
<Image Source="{Binding StatusImage}"></Image>
或者就像你没有上课一样.您可以在数据触发器之间进行选择:
Or as in your case that you haven't got a class. You could choose between a datatrigger:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Name="IsReadImage" Source="read.png"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsRead}" Value="False">
<Setter TargetName="IsReadImage" Property="Source" Value="unread.png"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
或者您可以使用值转换器:
Or you could use a value converter:
类:
public class IsReadImageConverter : IValueConverter
{
public Image ReadImage { get; set; }
public Image UnreadImage { get; set; }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!(value is bool))
{
return null;
}
bool b = (bool)value;
if (b)
{
return this.ReadImage
}
else
{
return this.UnreadImage
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
窗口资源:
<local:IsReadImageConverter ReadImage="Read.png" UnreadImage="Unread.png" x:Key="BoolImageConverter"/>
那么您的绑定将是:
ImageSource={Binding Path=IsRead,Converter={StaticResource BoolImageConverter}}"
应该一切正常.
这篇关于如何根据绑定值更改 WPF 数据网格行的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!