WPF-DataGrid设置各行变色

来源:互联网 发布:花椒网络直播 编辑:程序博客网 时间:2024/06/07 14:16

我在stackoverflow.com中找到的资料:我在stackoverflow.com中找到的资料:


he ItemContainerStyle is applied to the elements generated by the ItemsControl: ContentPresenter. The ContentPresenter will in turn contain whatever you put in your ItemTemplate. In the case of a ListBox, the ItemContainerStyle is applied to the generated ListBoxItem.

The AlternationCount is, based on what you posted, only available on these generated items. You cannot use the ItemContainerStyle to set the Grid's background, because the Grid is unknown to that Style.

The following would be ideal, but unfortunately ContentPresenter has no background property. It would work for a ListBox (with ListBoxItems) however.

<ItemsControl    ItemsSource="{Binding ObservableCollectionItems}"    AlternationCount="2">    <ItemsControl.ItemContainerStyle>        <Style TargetType="ContentPresenter">            <Style.Triggers>                <Trigger Property="ItemsControl.AlternationIndex" Value="0">                    <Setter Property="Background" Value="Red"></Setter>                </Trigger>                <Trigger Property="ItemsControl.AlternationIndex" Value="1">                    <Setter Property="Background" Value="Blue"></Setter>                </Trigger>            </Style.Triggers>        </Style>    </ItemsControl.ItemContainerStyle></ItemsControl>

So you end up writing a style for the grid which binds to the AlternationIndex of your parent ContentPresenter.

<DataTemplate DataType="{x:Type vm:ObservableCollectionItem}">    <Grid>        <Grid.Style>            <Style TargetType="Grid">                <Style.Triggers>                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}, Path=(ItemsControl.AlternationIndex)}" Value="0">                        <Setter Property="Background" Value="Red"/>                    </DataTrigger>                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}, Path=(ItemsControl.AlternationIndex)}" Value="1">                        <Setter Property="Background" Value="Blue"/>                    </DataTrigger>                </Style.Triggers>            </Style>        </Grid.Style>    </Grid></DataTemplate>

作者:Bubblewrap
网址:http://stackoverflow.com/questions/3567778/how-to-use-alternationindex-in-itemscontrols
--------------------------------------------------------------------------------------------------


我自己的实践:

<!-- DataGrid --><Style TargetType="DataGrid"><!-- 这里仅设置了AlternationCount --><!-- 其他样式可以自己编写 --><Setter Property="AlternationCount"  Value="2"/></Style><!--DataGridRow--><Style  TargetType="DataGridRow"><Style.Triggers><!--隔行换色--><Trigger Property="AlternationIndex" Value="0" ><Setter Property="Background" Value="Blue" /></Trigger><Trigger Property="AlternationIndex" Value="1" ><Setter Property="Background" Value="Red" /></Trigger><Trigger Property="IsMouseOver" Value="True"><Setter Property="Background" Value="Black"/><Setter Property="Foreground" Value="White"/></Trigger><Trigger Property="IsSelected" Value="True"><Setter Property="Foreground" Value="LightBlue"/></Trigger></Style.Triggers></Style>


希望对大家有所帮助。
0 0
原创粉丝点击