Q036. DataGrid のセルデータの垂直位置を上寄せから中央に変えたいのですが・・・
A. DataGrid のセルのクラスは DataGridCell です。このクラスのスタイルを設定します。
xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" <Style TargetType="my:DataGridCell" > <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type my:DataGridCell}"> <Grid Background="{TemplateBinding Background}"> <ContentPresenter VerticalAlignment="Center"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
なお以下のサンプルでは Height を設定してますが、DataGridRow の高さを Style で指定した場合、DataGridCell も DataGridRow の高さに合わせて設定しないとセルの境界線が正しく表示されないので注意が必要です。
xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" <Style TargetType="my:DataGridRow" > <Setter Property="Height" Value="20" /> </Style> <Style TargetType="my:DataGridCell" > <Setter Property="Height" Value="20" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type my:DataGridCell}"> <Grid Background="{TemplateBinding Background}"> <ContentPresenter VerticalAlignment="Center"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
関連記事 : ハンズオン ラボ: WPF の活用 〜 DataGrid 〜DataGrid にスタイルを付ける