How to change Background Color of ListBoxItem based on ListBoxItem Value

92 views Asked by At

I'm currently creating a Windows 8.1 Store app that acts as a task tracker. My current problem is that I want to change the Items listed in listview to represent their priority. I want to do this by Changing the Background color. IE: Red = High, Blue = Medium, and Green = Low.

The ListBoxItems themselves follow a Template, below is the relevant Text field I want to base changes from x:Name=priorityBind.

<StackPanel Orientation="Horizontal">
                    <TextBlock Text="Priority:  " Margin="10, 0, 10, 10" />
                    <TextBlock x:Name="priorityBind" Text="{Binding Path=Priority}" Margin="10, 0, 10, 10"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Notes:  " Margin="10, 0, 10, 10" />
                    <TextBlock Text="{Binding Path=Notes}" Margin="10, 0, 10, 10"/>
                </StackPanel>
            </StackPanel>
        </Grid>
    </DataTemplate>

All the examples I can find seem to be done with outdated version of the Store Apps. Setting up triggers seems to be the suggest route to go, but it no longer seems to work in the updated version. Below is a snippet of my ListView Code. How do I modify it so that it only updates the background color to green if the priority textbox holds a value of "Low"?

<ListView x:Name="eventsListView" Grid.Row="1" Grid.Column="1"
                  ItemTemplate="{StaticResource eventTemplate}">
            <ListView.Resources>
                <Style TargetType="ListViewItem">
                    <Style.Setters>
                        <Setter Property="BorderBrush"
                                Value="White" />
                        <Setter Property="BorderThickness"
                                Value="5"/>
                        <Setter Property="Background"
                                Value="Green"/>
                    </Style.Setters>
                </Style>
            </ListView.Resources>       
        </ListView>
1

There are 1 answers

0
Burak Kaan Köse On BEST ANSWER

You can specify which priority should use which tempalte by using ItemTemplateSelector

ItemTemplateSelector lets ListView to render different templates based on the property you give as parameter.This is the best practice solution AFAIK.