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>
希望对大家有所帮助。
- WPF-DataGrid设置各行变色
- 各行变色
- jQuery 各行变色,选中变色
- CSS各行变色
- ListView 各行变色写法
- 表格各行变色
- C# Windows Form中如何设置datagrid的各行颜色.
- wpf 设置datagrid 中的scrollviewer
- wpf datagrid设置行只读
- WPF中datagrid控件选中单元格变色或编辑单元格后变色
- 各行换色 鼠标悬停变色
- WPF中设置DataGrid的一些样式
- WPF DataGrid设置行的格式背景
- WPF设置DataGrid列的标题格式
- WPF中DataGrid设置列头样式
- WPF实战之DataGrid设置行高
- jquery表格动态添加及各行变色效果
- wpf datagrid
- 【醒目】【业界良心】【Public】资料包合集 公开
- Myeclipse Configure-->Convert to Maven Project
- iOS图片保真拉伸
- 转载本站文章请注明作者和出处 奇葩一朵朵 – http://www.cnblogs.com/season-huang/ ,请勿用于任何商业用途
- iOS学习笔记-----使用代理(Delegate)的页面传值
- WPF-DataGrid设置各行变色
- Android studio的代码迁移到eclipse中,没想象的那么难。
- 判断文件路径、url是否可用
- CSS3: animation实现简易幻灯片(轮播)
- 示例1-MVP
- NSString 相关使用
- linux 防火墙 设置
- json和Java互换
- POJ1135