Tag Archives: WPF DataGrid

WPF: How to Implement Table Style with DataGrid

resource

Define colors and styles


<SolidColorBrush x:Key="TextColorGeneral" Color="#a2a2a2" />


<Style TargetType="DataGridColumnHeader">
    <Setter Property="HorizontalContentAlignment" Value="Center" />
    <Setter Property="BorderBrush" Value="#a8a8a8" />
    <Setter Property="Background" Value="#f2f2f2" />
    <Setter Property="FontSize" Value="15" />
    <Setter Property="Padding" Value="5" />
    <Setter Property="Foreground" Value="{StaticResource TextColorGeneral}" />
    <Setter Property="BorderThickness" Value="0,0,1,1" /> // Important!
</Style>

Set dataGrid properties


<DataGrid
        Grid.Row="5"
        d:ItemsSource="{StaticResource testItems}"
        AutoGenerateColumns="False"
        BorderBrush="#a8a8a8"
        CanUserAddRows="False"
        CanUserReorderColumns="False"
        CanUserResizeColumns="False"
        HeadersVisibility="Column"
        ItemsSource="{Binding CalibrationItems}"
        SelectionMode="Single"
        SelectionUnit="Cell"
        Style="{StaticResource BaseStyle}"
        BorderThickness="1,1,0,0" // Important!
        HorizontalGridLinesBrush="#a8a8a8" // Important!
        VerticalGridLinesBrush="#a8a8a8"> // Important!
</DataGrid>

renderings

image