如何在WPF中为列表视图提供交替行背景颜色和交替列背景颜色?

时间:2022-01-04 06:50:48

And also i want to color the background of headers. Please help.

而且我想要标题的背景颜色。请帮忙。

Thanks

谢谢

2 个解决方案

#1


10  

You need to set up a style with the AlternationIndex set. This page gives an example

您需要设置AlternationIndex集的样式。这个页面给出了一个例子

(defining the style)

(定义风格)

<Style x:Key="CustomListViewItemStyle" TargetType="{x:Type ListViewItem}">
    <Style.Triggers>
        <Trigger Property="ItemsControl.AlternationIndex" Value="0">
            <Setter Property="Background" Value="#2C2C2C"></Setter>
        </Trigger>
        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
            <Setter Property="Background" Value="#262626"></Setter>
        </Trigger>
    </Style.Triggers>
</Style>

(using the defined style)

(使用定义的样式)

<ListView ItemContainerStyle="{DynamicResource CustomListViewItemStyle}"
          AlternationCount="2">
    ...
</ListView>

#2


0  

' Snipet demonstrating how to alternate colors per N rows of DataGrid using code (VB.Net without Xaml). Uses .Net 3.5 feature of AlternationIndex, AlternationCount
' Assumes DataGrid1 is defined in Window1
' Alternation can be on any size (row count)
       Dim BackgroundStyle As New Style
        BackgroundStyle.TargetType = GetType(DataGridRow)
        For i As Integer = 1 To alternationRows
            Dim tr As New Trigger
            tr.Property = ItemsControl.AlternationIndexProperty
            tr.Value = i - 1
            Dim st As New Setter
            st.Property = BackgroundProperty
            st.Value = New SolidColorBrush(Color.FromRgb(CByte(&HFF - i * 8), CByte(&HFF - i * 8), CByte(&HFF - i * 8)))
            tr.Setters.Add(st)
            BackgroundStyle.Triggers.Add(tr)
        Next
        DataGrid1.ItemContainerStyle = BackgroundStyle
        DataGrid1.AlternationCount = BackgroundStyle.Triggers.Count

#1


10  

You need to set up a style with the AlternationIndex set. This page gives an example

您需要设置AlternationIndex集的样式。这个页面给出了一个例子

(defining the style)

(定义风格)

<Style x:Key="CustomListViewItemStyle" TargetType="{x:Type ListViewItem}">
    <Style.Triggers>
        <Trigger Property="ItemsControl.AlternationIndex" Value="0">
            <Setter Property="Background" Value="#2C2C2C"></Setter>
        </Trigger>
        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
            <Setter Property="Background" Value="#262626"></Setter>
        </Trigger>
    </Style.Triggers>
</Style>

(using the defined style)

(使用定义的样式)

<ListView ItemContainerStyle="{DynamicResource CustomListViewItemStyle}"
          AlternationCount="2">
    ...
</ListView>

#2


0  

' Snipet demonstrating how to alternate colors per N rows of DataGrid using code (VB.Net without Xaml). Uses .Net 3.5 feature of AlternationIndex, AlternationCount
' Assumes DataGrid1 is defined in Window1
' Alternation can be on any size (row count)
       Dim BackgroundStyle As New Style
        BackgroundStyle.TargetType = GetType(DataGridRow)
        For i As Integer = 1 To alternationRows
            Dim tr As New Trigger
            tr.Property = ItemsControl.AlternationIndexProperty
            tr.Value = i - 1
            Dim st As New Setter
            st.Property = BackgroundProperty
            st.Value = New SolidColorBrush(Color.FromRgb(CByte(&HFF - i * 8), CByte(&HFF - i * 8), CByte(&HFF - i * 8)))
            tr.Setters.Add(st)
            BackgroundStyle.Triggers.Add(tr)
        Next
        DataGrid1.ItemContainerStyle = BackgroundStyle
        DataGrid1.AlternationCount = BackgroundStyle.Triggers.Count