Wpf ListViewItem Background binding to enum
I´ve got a ListView which is bound to the ObservableCollection mPersonList. The Class Person got an enum Sex. What i want to do is to set the background of the Li开发者_如何学运维stViewItem to green if the person is male and to red if the person is female.
Thanks for the answers!
i tried it like this but whats wrong with it?
<Style x:Key="CustomListViewItemStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="Background" Value="{Binding Path=Status, Converter={StaticResource sexEnumToColor}}" />
</Style>
and:
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
}
Adding the binding explicitly to the data template for the ListViewItem
won't work if you're not using a data template. In that case, you'll need to get the style to work, and to get it to work, you need to know why it doesn't.
What's wrong with your style is that you've assigned a key to it. When any element is created, WPF searches the resource dictionary for a Style
object whose key is that element's type. If it finds one, that's the style that it applies. If it doesn't, no style gets applied.
If you specify TargetType
in your style declaration but omit x:Key
, the key that gets assigned when it's added to the resource dictionary is that type. But since you've explicitly specified a key for your style, that's the key that gets assigned instead. So WPF never finds it, and it doesn't get applied.
Since you don't want to globally add this style to every ListViewItem
in your window, the thing to do is probably to add it to the ListView
's resource dictionary:
<ListView ItemsSource="{DynamicResource Data}">
<ListView.Resources>
<local:SexToColorConverter x:Key="SexConverter" />
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Background"
Value="{Binding Path=Sex, Converter={StaticResource SexConverter}}" />
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Header="Name"
DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Sex"
DisplayMemberBinding="{Binding Sex}" />
</GridView>
</ListView.View>
</ListView>
You can bind sex property with converter. Something like this:
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding name}" Background="{Binding sex, Converter={StaticResource converterSexToColor}}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
and converter:
public class ConverterSexToColor : IValueConverter
{
public object Convert(object value, Type targeTtype, object parameter, System.Globalization.CultureInfo culture)
{
return (Sex)value == Sex.Male ? Brushes.Green : Brushes.Red;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Use a DataTrigger in your template of the Person class.
If you post part of the class definition and the current template, we could complete it.
精彩评论