wpf datagrid样式
时间: 2023-09-10 07:10:39 浏览: 179
WPFDataGrid样式
WPF DataGrid 可以通过样式(Style)来自定义其外观和行为。以下是一个基本的样式示例:
```xml
<Window.Resources>
<Style TargetType="{x:Type DataGrid}">
<Setter Property="Background" Value="White"/>
<Setter Property="BorderBrush" Value="Gray"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="RowHeaderWidth" Value="0"/>
<Setter Property="HeadersVisibility" Value="Column"/>
<Setter Property="RowBackground" Value="#F0F0F0"/>
<Setter Property="AlternatingRowBackground" Value="#E0E0E0"/>
<Setter Property="AutoGenerateColumns" Value="False"/>
<Setter Property="CanUserAddRows" Value="False"/>
<Setter Property="CanUserDeleteRows" Value="False"/>
<Setter Property="SelectionMode" Value="Single"/>
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="FontFamily" Value="Segoe UI"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Foreground" Value="#333333"/>
<Setter Property="GridLinesVisibility" Value="None"/>
<Setter Property="ColumnHeaderHeight" Value="30"/>
<Setter Property="RowHeight" Value="30"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGrid}">
<Border x:Name="border"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="2">
<ScrollViewer x:Name="DG_ScrollViewer"
Focusable="false">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<DataGridColumnHeadersPresenter Grid.Row="0"
Grid.Column="0"
Margin="0,0,0,5"
x:Name="PART_ColumnHeadersPresenter"
Visibility="{TemplateBinding HeadersVisibility}"/>
<ScrollContentPresenter x:Name="PART_ScrollContentPresenter"
Grid.Row="1"
Grid.Column="0"
CanContentScroll="{TemplateBinding CanContentScroll}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
ContentStringFormat="{TemplateBinding ContentStringFormat}"
Focusable="false"/>
</Grid>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
```
该样式定义了 DataGrid 的各种属性和行为,例如背景、边框、行高、列头高度等。可以根据需求修改这些值来自定义 DataGrid 的样式。
阅读全文