Moauayad Al Zein
Moauayad Al Zein

Reputation: 57

how to dynamically change listview items styles during runtime?

I am using VS2010 - wpf - C#

I have a listview that shows securities prices and I want to style the listview rows in three different ways : green - red - and orange

I don't know how to write a C# code that would affect the styles of these listview items during runtime under some conditions that I have ???

I don't know if my question needs some more explanation but if it does please let me know

Best Regards to everyone

Upvotes: 0

Views: 2163

Answers (1)

Rachel
Rachel

Reputation: 132548

Why do you want to do it at runtime instead of in the XAML?

I am assuming the red/green/orange has some kind of meaning. Setup a DataTrigger on the Listbox's ItemContainerStyle which modifies the color based on a value.

Here's an example using a property named Priority on the ListView items

<ListView>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Foreground" Value="Green" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Priority}" Value="2">
                    <Setter Property="Foreground" Value="Orange" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Priority}" Value="3">
                    <Setter Property="Foreground" Value="Red" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

Upvotes: 1

Related Questions