WPF Listview 3 个差异列表的不同替代行颜色..?

发布于 2024-10-05 03:58:07 字数 200 浏览 0 评论 0原文

如何为 WPF 列表视图设置备用行颜色。 如果我只有 1 个列表,我可以在 XAML 中设置,但就我而言,需要根据列表更改备用行颜色。 例如,我有 3 个差异列表...
1) 按公司名称订购,
2) 按部门排序
3) 按市值排序
每个列表都应该有自己的备用行颜色。
我怎样才能做到这一点(在 C# 或 XAML 文件中)。任何想法/建议将不胜感激

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 need 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

英雄似剑 2024-10-12 03:58:07

无论您对列表执行什么操作,这都应该有效,因为 ItemsControl.AlternationIndex 是依赖项属性并且应该更新:

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

用于测试项目更改的代码隐藏:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文