开发者

WPF Listview different alternate row colors for 3 diff lists..?

开发者 https://www.devze.com 2023-01-27 00:58 出处:网络
How do i set alternate row colors for WPF Listview. If i have only 1 list I can set in XAML, but in my case, the alternate row colors n开发者_如何学Goeed to be changed based on the list.

How do i set alternate row colors for WPF Listview. If i have only 1 list I can set in XAML, but in my case, the alternate row colors n开发者_如何学Goeed to be changed based on the list. For example,i have 3 diff lists...

1) order by Company name,

2) order by Sector

3) order by Market value

Each list should have their own alternate row colors.

How can i do that(in C# or in XAML file).Any Ideas/Suggestions would be aprreciated


This should work no matter what you do to the list since ItemsControl.AlternationIndex is a dependency property and should get updated:

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style TargetType="ListBoxItem">
        <Style.Triggers>
            <Trigger Property="ItemsControl.AlternationIndex"  Value="0">
                <Setter Property="Background" Value="AliceBlue" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<StackPanel>  
    <ListBox Name="bob" AlternationCount="2">
        <ListBoxItem Content="Herald"/>
        <ListBoxItem Content="Kumar" />
        <ListBoxItem Content="Bill" />
        <ListBoxItem Content="Dudley" />
        <ListBoxItem Content="Jack" />
    </ListBox>
    <Button Click="Button_Click">boo</Button>
</StackPanel>

Code Behind to test changes to items:

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    Dim item1 As ListBoxItem = bob.Items(4)
    bob.Items.Remove(item1)
    bob.Items.Insert(0, item1)
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号