【发布时间】:2011-01-30 07:19:00
【问题描述】:
我有一个绑定到 ObservableCollection mPersonList 的 ListView。 Class Person 有一个枚举 Sex。我想要做的是,如果这个人是男性,则将 ListViewItem 的背景设置为绿色,如果这个人是女性,则将其设置为红色。
感谢您的回答!
我试过这样但是有什么问题?
<Style x:Key="CustomListViewItemStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="Background" Value="{Binding Path=Status, Converter={StaticResource sexEnumToColor}}" />
</Style>
和:
public class SexEnumToColor : IValueConverter
{
#region IValueConverter Member
object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Sex tempSex = (Sex)value;
Brush retval;
switch (tempSex )
{
case Sex.Male:
retval = Brushes.Blue;
break;
case Sex.Female:
retval = Brushes.Red;
break;
default:
retval = Brushes.Black;
break;
}
return retval;
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
【问题讨论】:
标签: wpf listview binding enums