Home > database >  How To Add Style To WPF (C#) Datagrid? (Solved)
How To Add Style To WPF (C#) Datagrid? (Solved)

Time:09-12

I have followed a tutorial which told me to create a copy of the DataGrid and then add some style on it. This file is currently named "DataGrid.xaml" however I do not see the changes on my actual DataGrid, it still looks like the classic DataGrid in WPF (C#)...How Do I go about it?

CodePudding user response:

You can define the style of the Grid inside the Grid's xaml code as follows

<DataGrid
    AutoGenerateColumns="True"
    ItemsSource="{Binding Items}">
    <DataGrid.Resources>
        <Style TargetType="DataGrid">
            <Setter Property="IsReadOnly" Value="True" />
            <Setter Property="AutoGenerateColumns" Value="False" />
            <Setter Property="CanUserDeleteRows" Value="False" />
            <Setter Property="CanUserAddRows" Value="False" />
            <Setter Property="CanUserReorderColumns" Value="False" />
            <Setter Property="FontSize" Value="14" />
            <Setter Property="RowHeight" Value="40" />
            <Setter Property="ColumnWidth" Value="*" />
        </Style>
    </DataGrid.Resources>
</DataGrid>
  • Related