I have:
public class Person
{
String name { get; set; }
String address { get; set; }
bool isMarried { get; set; }
}
My datagrid gets populated with a list of persons.
I want to have a custom column where icon-1.jpg is displayed when isMarried
is true
and icon-2.jpg is displayed when isMarried
is false
.
How do I do开发者_如何转开发 this in WPF ? Any ideas ?
I know how to do a custom column but I do not know how to assoc the two states of isMarried
with icon-1.jpg and icon-2.jpg.
You could do this with a DataTrigger in your custom column:
<DataGridTemplateColumn Header="Married">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image x:Name="IMG" Source="married_image" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=isMarried}" Value="False">
<Setter Property="Source" Value="not_married_image" TargetName="IMG"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
You can use an IValueConveter to convert from a Boolean value to an Uri (Uri is what you need for image source).
public class MarriedConverter : IValueConverter
{
public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
{
if ((value == null) || !(value is bool))
return null;
bool isMarried = (bool)value;
if (isMarried)
return new Uri(#1);
else
return new Uri(#2);
}
public Object ConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
精彩评论