Tag: WPF/DataGrid/スタイル
<Style TargetType="DataGrid"> <!-- Make the border and grid lines a little less imposing --> <Setter Property="BorderBrush" Value="#DDDDDD" /> <Setter Property="HorizontalGridLinesBrush" Value="#DDDDDD" /> <Setter Property="VerticalGridLinesBrush" Value="#DDDDDD" /> <Setter Property="RowStyle"> <Setter.Value> <Style TargetType="DataGridRow"> <Style.Triggers> <!-- Highlight a grid row as the mouse passes over --> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="Lavender" /> </Trigger> </Style.Triggers> </Style> </Setter.Value> </Setter> <Setter Property="CellStyle"> <Setter.Value> <Style TargetType="DataGridCell"> <Style.Triggers> <!-- Highlight selected rows --> <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="Lavender" /> <Setter Property="BorderBrush" Value="Lavender" /> <Setter Property="Foreground" Value="Black" /> </Trigger> </Style.Triggers> <!-- Add some padding around the contents of a cell --> <Setter Property="Padding" Value="4,3,4,3" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="DataGridCell"> <Border Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}"> <ContentPresenter /> </Border> </ControlTemplate> </Setter.Value> </Setter> <Setter Property="FocusVisualStyle"> <Setter.Value> <Style TargetType="Control"> <Setter Property="BorderBrush" Value="Transparent" /> </Style> </Setter.Value> </Setter> </Style> </Setter.Value> </Setter> </Style>
<Style x:Key="RightTextBlock" TargetType="TextBlock"> <Setter Property="HorizontalAlignment" Value="Right" /> </Style>
<DataGridTextColumn Header="サイズ" ElementStyle="{StaticResource RightTextBlock}"></DataGridTextColumn>
※次のようにCellStyleを変更すると、その列だけデフォルトのスタイルに戻ってしまうのでだめ。
<DataGridTextColumn Header="サイズ"> <DataGridTextColumn.CellStyle> <Style TargetType="{x:Type DataGridCell}"> <Setter Property="HorizontalAlignment" Value="Right"/> </Style> </DataGridTextColumn.CellStyle>