Search code examples
c#wpfxamlwpfdatagrid

How to set Style to WPFToolkit DataGrid?


I am new to WPF, am developing using C# and .NET3.5. I have a WPFToolkit's DataGrid added to my window. I am not able to figure out how to set Style to the Column Header and Rows? Rows are added dynamically.

        <my:datagrid name="myGrid" xmlns:my="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit" itemssource="{Binding }" autogeneratecolumns="False">
                 SelectionMode="Extended" SelectionUnit="FullRow" CanUserReorderColumns="False" 
                 ColumnHeaderHeight="42" Background="#FFF7F7F7" BorderBrush="Transparent" 
                 HorizontalGridLinesBrush="#FFEAEAEA" VerticalGridLinesBrush="#FFEAEAEA" 
                 HeadersVisibility="Column" RowHeaderWidth="0" HorizontalContentAlignment="Center" 
                 VerticalContentAlignment="Center" ClipboardCopyMode="None" MinRowHeight="28" 
                 Rowremoved="#FFF7F7F7" RowDetailsVisibilityMode="Visible" RowHeight="28" 
                 DataContextChanged="serverGrid_DataContextChanged">
        <my:datagrid.columns>
            <my:datagridtextcolumn header="Enabled" width="120" binding="{Binding Path=Name}" />
            <my:datagridtextcolumn header="Enabled" width="70" binding="{Binding Path=Country}" />
            <my:datagridtextcolumn header="Enabled" width="100" binding="{Binding Path=Description}" />
        </my:datagrid.columns>

    </my:datagrid>

In the Resources have added code for Style :

    <!-- DataGridColumnHeader-->
    <Style x:Key="ColumnHeaderStyle" TargetType="{x:Type Thumb}">
        <setter property="Background" value="#9DCFD0" />
        <setter property="FontFamily" value="Arial Rounded MT" />
        <setter property="FontSize" value="14" />
        <setter property="FontWeight" value="Bold" />
        <setter property="Foreground" value="#00545B" />
        <setter property="VerticalContentAlignment" value="Center" />
        <setter property="HorizontalContentAlignment" value="Center" />
    </Style>

In TargetType of Style, am not able to set as my:DataGridColumnHeader or just DataGridColumnHeader. It says "... not found". In my:DataGridTextColumn I guess HeaderStyle is the property to set the style. But am able to define Style for the same.

Also how to set style for Rows added dynamically? Where am I going wrong? Any help is highly appreciated.


Solution

  • you can do like this:

    First is the namespace to header:

    xmlns:Custom="http://schemas.microsoft.com/wpf/2008/toolkit"
    

    then this is style:

    <Style x:Key="DataGridColumnHeaderStyle" TargetType="{x:Type Custom:DataGridColumnHeader}">
            <Setter Property="HorizontalAlignment" Value="Center"></Setter>
            <Setter Property="Foreground" Value="#654b24"></Setter>
            <Setter Property="FontWeight" Value="bold"></Setter>
            <Setter Property="Height" Value="30"></Setter>
            <Setter Property="Background">
                <Setter.Value>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#f7f3de" Offset="0.1"/>
                        <GradientStop Color="#Fcfcfc" Offset="1"/>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
        </Style>