将图标添加到 WPF 数据网格中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14646548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding an icon into WPF datagrid
提问by user2025830
I want to adding an icon in each row of my datagrid. the columns are generated automatically and i have added an column with a datagridtemplatecolumn to show an icon in the first column.
我想在数据网格的每一行中添加一个图标。这些列是自动生成的,我添加了一个带有 datagridtemplatecolumn 的列,以在第一列中显示一个图标。
this is my xaml code to show the icon:
这是我显示图标的 xaml 代码:
<DataGrid ItemsSource="{Binding User.myDataTable}" IsReadOnly="True" FrozenColumnCount="1">
<DataGrid.Columns>
<DataGridTemplateColumn x:Name="IconHeader" Header="" CanUserResize="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="myImage" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
that works well. now i want to change the icon if in one column of the row is a condition is true. for example. if column 11 is value "true" then icon 1 and if value "false" then icon2.
效果很好。现在我想更改图标,如果在行的一列中条件为真。例如。如果第 11 列的值为“true”,则为图标 1,如果值为“false”,则为 icon2。
can i use the loadingrow event from the datagrid to do this and how can i do this with mvvm? or is there another way to do something like this?
我可以使用 datagrid 中的 loadingrow 事件来执行此操作吗?我如何使用 mvvm 执行此操作?或者有另一种方法来做这样的事情吗?
回答by John Bowen
The simplest thing to do here is use a DataTrigger in your CellTemplate that will fire based on a binding to the column data:
这里要做的最简单的事情是在您的 CellTemplate 中使用 DataTrigger,它将根据与列数据的绑定触发:
<DataTemplate>
<Image Source="myImage1" x:Name="img" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=[11]}" Value="False">
<Setter TargetName="img" Property="Source" Value="myImage2" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
DataRow has both int and string indexers for getting a column by index or name so for your binding path you can use []with either the column index ([3]) or the column name ([MyColumn]).
DataRow 具有 int 和 string 索引器,用于按索引或名称获取列,因此对于您的绑定路径,您可以[]与列索引 ( [3]) 或列名 ( [MyColumn]) 一起使用。
回答by Marcel
I believe the best way would be to implement a IValueConverter. If column11 is true, show the one image, if it's false, then show the other image.
我相信最好的方法是实现 IValueConverter。如果 column11 为 true,则显示一个图像,如果它为 false,则显示另一个图像。
IValueConverter : http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx
IValueConverter:http: //msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx
Something like :
就像是 :
public class IconConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((bool)value == true)
{
// column11 = true, so show icon 1
return image1;
}
else
{
return image2;
}
}
}
回答by Florian Gl
This sounds like a job for converters. Just bind the Source-property of your Image to the Property you want:
这听起来像是转换器的工作。只需将您的图像的源属性绑定到您想要的属性:
<Image Source="{Binding Path=BoolProp, Converter={StaticResource BoolToImageConv}}"
Your converter could look like
您的转换器可能看起来像
public class BoolToImageConverter:IValueConverter
{
public string FalsePath { get; set; }
public string TruePath { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (System.Convert.ToBoolean(value))
{
return new BitmapImage(new Uri(TruePath));
}
return new BitmapImage(new Uri(FalsePath));
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
In the resources of your Window/UserControl you have to refer your Converter.
在您的 Window/UserControl 的资源中,您必须引用您的转换器。
<Window.Resources>
<conv:BoolToImageConverter FalsePath="pathforimageiffalse" TruePath="pathforimageiftrue" x:Key="BoolToImageConv"/>
</Window.Resources>

