WPF DataGrid常用属性记录

BeginEdit :使DataGrid进入编辑状态。

CancelEdit :取消DataGrid的编辑状态。

CollapseRowGroup :闭合DataGrid的行分组。

CommitEdit :确认DataGrid的编辑完成。

ExpandRowGroup :展开DataGrid的行分组。

GetGroupFromItem :从具体Item中得到分组。

ScrollIntoView :滚动DataGrid视图。

组件常用属性:

AlternatingRowBackground :获取或设置一个笔刷用来描绘DataGrid奇数行的背景。

AreRowDetailsFrozen :获取或设置一个值用来判断是否冻结每行内容的详细信息。

AreRowGroupHeadersFrozen :获取或设置一个值用来判断是否冻结分组行的头部。

AutoGenerateColumns :获取或设置一个值用来判断是否允许自动生成表列。

CanUserReorderColumns :获取或设置一个值用来判断是否允许用户重新排列表列的位置。

CanUserSortColumns :获取或设置一个值用来判断是否允许用户按列对表中内容进行排序。

CellStyle :获取或设置单元格的样式。

ColumnHeaderHeight :获取或设置列头的高度。

ColumnHeaderStyle :获取或设置列头的样式。

Columns :获取组件中包含所有列的集合。

ColumnWidth :获取或设置列宽。

CurrentColumn :获取或设置包含当前单元格的列。

CurrentItem :获取包含当前单元格且与行绑定的数据项。

DragIndicatorStyle :获取或设置当拖曳列头时的样式。

DropLocationIndicatorStyle :获取或设置呈现列头时的样式。

FrozenColumnCount :获取或设置冻结列的个数。

GridLinesVisibility :获取或设置网格线的显示形式。

HeadersVisibility :获取或设置行头及列头的显示形式。

HorizontalGridLinesBrush :获取或设置水平网格线的笔刷。

HorizontalScrollBarVisibility :获取或设置水平滚动条的显示样式。

IsReadOnly :获取或设置DataGrid是否为只读。

MaxColumnWidth :获取或设置DataGrid的最大列宽。

MinColumnWidth :获取或设置DataGrid的最小列宽。

RowBackground :获取或设置用于填充行背景的笔刷。

RowDetailsTemplate :获取或设置被用于显示行详细部分的内容的模板。

RowDetailsVisibilityMode :获取或设置一个值用以判定行详细部分是否显示。

RowGroupHeaderStyles :获取呈现行分组头部的样式。

RowHeaderStyle :获取或设置呈现行头的样式。

RowHeaderWidth :获取或设置行头的宽度。

RowHeight :获取或设置每行的高度。

RowStyle :获取或设置呈现行时的样式。

SelectedIndex :获取或设置当前选中部分的索引值。

SelectedItem :获取或设置与当前被选中行绑定的数据项。

SelectedItems :获取与当前被选中的各行绑定的数据项们的列表(List)。

SelectionMode :获取或设置DataGrid的选取模式。

VerticalGridLinesBrush :获取或设置垂直网格线的笔刷。

VerticalScrollBarVisibility :获取或设置垂直滚动条的显示样式。

组件常用事件:

BeginningEdit :发生于一个单元格或行进入编辑模式之前。

CellEditEnded :发生于一个单元格编辑已被确认或取消。

CellEditEnding :发生于一个单元格正在结束编辑时。

CurrentCellChanged :发生于一个单元格成为当前单元格时。

PreparingCellForEdit :发生于在DataGridTemplateColumn下的单元格进入编辑模式时。

SelectionChanged :发生于当SelectedItem或SelectedItems属性值改变时。

1.DataGrid隔行变色

RowBackground和AlternatingRowBackground设置一排交替行的背景。AlternationCount是将用于行的样式或颜色的总数

1 <DataGrid Name="dg" RowHeaderWidth="50" AlternationCount="2" AlternatingRowBackground="#F4F4F4"  AutoGenerateColumns="False" Grid.Row="1" HeadersVisibility="All"  Margin="4" >
2                     <DataGrid.Columns>
3                         <DataGridTextColumn Binding="{Binding Name}" Header="Name"/>
4                     </DataGrid.Columns>
5                 </DataGrid>

如果设置AlternatingRowBackground刷,将被分配到行,其中(rownumber%AlternationIdex)== 1

 1 <Style x:Key="DataGridDemoRowStyle"  
 2 TargetType="{x:Type Custom:DataGridRow}">
 3 <Style.Triggers>
 4 <Trigger Property="AlternationIndex" Value="2" >
 5  <Setter Property="Background" Value="{StaticResource RowBackgroundAlternationIndex2Brush}" />
 6 </Trigger>
 7 <Trigger Property="AlternationIndex" Value="3">
 8 <Setter Property="Background" Value="{StaticResource RowBackgroundAlternationIndex3Brush}" />
 9 </Trigger>
10 </Style.Triggers>
11 </Style>

请注意对于上面的样式,有目的的,我只覆盖AlternationIndex= 2,3。对于AlternationIndex= 0时,它使用RowBackground。

对于AlternationIndex= 1,它使用从DataGrid中AlternatingRowBackground的。

2.DataGrid的 ErrorTemplate 属性

 1 <Style x:Key="{x:Type DataGridRow}" TargetType="{x:Type DataGridRow}">
 2         <Setter Property="Background" Value="Transparent" />
 3         <Setter Property="SnapsToDevicePixels" Value="true"/>
 4         <Setter Property="Validation.ErrorTemplate" Value="{x:Null}" />
 5         <Setter Property="ValidationErrorTemplate">
 6             <Setter.Value>
 7                 <ControlTemplate>
 8                     <TextBlock Margin="2,0,0,0" VerticalAlignment="Center" Foreground="#FFdc000c" Text="!" />
 9                 </ControlTemplate>
10             </Setter.Value>
11         </Setter>
12 </Style>

上面这段XAML设置了DataGridRow的 ValidationErrorTemplate 为RowHeader通过模板

 1 <Style x:Key="{x:Type DataGridRowHeader}" TargetType="{x:Type DataGridRowHeader}">
 2         <Setter Property="Background" Value="{DynamicResource DataGridHeaderBackground}" />
 3         <Setter Property="Foreground" Value="{DynamicResource DataGridHeaderForeground}" />
 4         <Setter Property="BorderBrush" Value="{DynamicResource DataGridGridLines}" />
 5         <Setter Property="BorderThickness" Value="0,0,0,1" />
 6         <Setter Property="Width" Value="16"/>
 7         <Setter Property="Template">
 8             <Setter.Value>
 9                 <ControlTemplate TargetType="{x:Type DataGridRowHeader}">
10                     <Grid>
11                         <Border Background="{TemplateBinding Background}"
12                                 BorderBrush="{TemplateBinding BorderBrush}"
13                                 BorderThickness="{TemplateBinding BorderThickness}"
14                                 Padding ="{TemplateBinding Padding}">
15 
16                             <StackPanel Orientation="Horizontal">
17                                 <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/>
18                                 <Control SnapsToDevicePixels="false"
19                                          Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, Path=(Validation.HasError), Converter={StaticResource BooleanToVisibilityConverter}}"
20                                          Template="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, Path=ValidationErrorTemplate}" />
21                             </StackPanel>
22                         </Border>
23                         <Thumb x:Name="PART_TopHeaderGripper"
24                                VerticalAlignment="Top"
25                                Style="{StaticResource RowHeaderGripperStyle}"/>
26                         <Thumb x:Name="PART_BottomHeaderGripper"
27                                VerticalAlignment="Bottom"
28                                Style="{StaticResource RowHeaderGripperStyle}"/>
29                     </Grid>
30                 </ControlTemplate>
31             </Setter.Value>
32         </Setter>
33 
34         <Style.Triggers>
35             <Trigger Property="IsMouseOver" Value="True">
36                 <Setter Property="Background" Value="{DynamicResource DataGridHeaderBackgroundHover}" />
37                 <Setter Property="Foreground" Value="{DynamicResource DataGridHeaderForegroundHover}" />
38             </Trigger>
39             <Trigger Property="IsPressed" Value="True">
40                 <Setter Property="Background" Value="{DynamicResource DataGridHeaderBackgroundPressed}" />
41                 <Setter Property="Foreground" Value="{DynamicResource DataGridHeaderForegroundPressed}" />
42             </Trigger>
43             <Trigger Property="IsRowSelected" Value="True">
44                 <Setter Property="Background" Value="{DynamicResource DataGridHeaderBackgroundSelected}" />
45                 <Setter Property="Foreground" Value="{DynamicResource DataGridHeaderForegroundSelected}" />
46             </Trigger>
47         </Style.Triggers>
48     </Style>

主对于错误验证主要是 当有错误的时候Control显示,并获取上面设置的DataGridRow的错误验证模板为Control的模板,当没有错误时就隐藏作为验证显示的Control

3.DataGrid过滤

猜你喜欢

转载自blog.csdn.net/weixin_41619400/article/details/82415714