Rabi
Rabi

Reputation: 135

How to set different background according to one property?

I have ListView which ItemSource bindend to ObservableCollection<Period> where Period is

 public class Period : INotifyPropertyChanged
 {
    //some stuff
    //

    public Status PeriodStatus
    {
        get;
        set;
    }

    #region PropertyChangedEventHandler members

    public void SendPropertyChanged(string name)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(name));
    }

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion 
}

public enum Status
{
    None,
    Added,
    Deleted,
    Edited
}

And i want set background of each ListViewItem in this order : added-green/deleted-red/edited-yellow/none-default . Found at here many solutions, but didn't provide my solution. If there is exist question , please comment and i will close this

[EDIT] I wanted to use DataTemplate in this way: create template which create binding with Background property and Status in Period which uses converter. But didn't know how to keep rest of design

Upvotes: 0

Views: 1134

Answers (3)

GameAlchemist
GameAlchemist

Reputation: 19294

I find it more easy to have a dedicated PeriodStatusColor readonly property, less 'pure xaml', yes, but less code, and all code in the same place. So this property just returns color for current PeriodStatus. On PeriodStatus change, raise also a PeriodStatusColor PropertyChanged. Use static frozen color, and maybe use a PeriodStatus --> Color static Dictionnary to have clean code.

Upvotes: 0

STiLeTT
STiLeTT

Reputation: 1073

The simplest one is to use Triggers in your ListView.ItemContainerStyle.

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=PeriodStatus}" Value="Added">
                <Setter Property="Background" Value="Green" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=PeriodStatus}" Value="Deleted">
                <Setter Property="Background" Value="Red" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=PeriodStatus}" Value="Edited">
                <Setter Property="Background" Value="Yellow" />
            </DataTrigger>
        <Style.Triggers>
    <Style>
<ListView.ItemContainerStyle>

This is a sample code: you might need to add a namespace with your enum to use it in XAML.

Upvotes: 1

Please use DataTriggers for the Background property in the Style of ListViewItem (example in this question: you don't need the converter, use the enum values instead of integer values).

<ListView>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=PeriodStatus}" Value="Added">
                    <Setter Property="Background" Value="Green" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=PeriodStatus}" Value="Deleted">
                    <Setter Property="Background" Value="Red" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=PeriodStatus}" Value="Edited">
                    <Setter Property="Background" Value="Yellow" />
                </DataTrigger>
            <Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

Upvotes: 1

Related Questions