views:

325

answers:

3

Hi Guys 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 ListViewItem 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
}
A: 

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.

Timores
A: 

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();
    }
}
mt_serg
Thx for that answer but this changes the backgroundcolor of the textbox. how can i do this for changing the backgroundcolor of one listviewitem?
Christian
in this case textbox is just one element in datatemplate. So changing bg of text box is the same as changing bg of listviewitem. If you have another data template you can wrap your template by <StackPanel> (for example) and bind sex prorepty to it background.
mt_serg
+1  A: 

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>
Robert Rossney