Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

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.

HTML
        <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 ?

Any help is highly appreciated.
Posted

1 solution

First of all you should define your namespace in an element that contains your datagrid, and not in the datagrid itself. I always declare namespaces in the Window/UserControl declaration. Then you should be able to see DataGridColumnHeader as a Type in your style declaration. Then in you Columns declarations you need to explicitly set the style to the correct resource by using the StaticResource declaration like this:-

XML
<DataGridTextColumn Header="Enabled" Width="120" Binding="{Binding Path=Name}" HeaderStyle="{StaticResource ColumnHeaderStyle}"/>
.

Styling the Rows is the same thing. Declare the style in your resources with a Key, and specifically set the row style to that style in your datagrid declaration like this:-

XML
<Style x:Key="RowStyle" TargetType="{x:Type DataGridRow}">
                <!--Setters go here-->
            </Style>



XML
<...ClipboardCopyMode="None" MinRowHeight="28"
                     RowDetailsVisibilityMode="Visible" RowHeight="28"
                     DataContextChanged="serverGrid_DataContextChanged" RowStyle="{StaticResource RowStyle}"


Hope this helps
 
Share this answer
 
Comments
All Time Programming 7-Sep-11 7:56am    
Hi Wayne, I already added namespace in Window declaration. But yet in Style, I can't access my:DataGridColumnHeader. It gives me "The type reference cannot find a public type named 'DataGridColumnHeader'. "

To know if I can access it or not, in the main Panel, I tried typing
<my: &="" can="" have="" access="" to="" only="" datagrid,="" datagridcell,="" datagridcellspanel,="" datagridheaderborder="" and="" datarow="" in="" respect="" datagrid="" classes.=""
i="" tried="" adding="" inside="" <my:datagrid.resources=""> but can't access it here also - shows same error msg.
If I use DataGridCell, then all row's cell are set with the style and col headers only when mouseOver, else normal headers only. I want to have seperate style for Headers and Rows. How do I achieve Style with Col Headers ?
Wayne Gaylard 7-Sep-11 8:17am    
Instead of trying TargetType="{x:Type my:DataGridColumnHeader}" just type TargetType="my:DataGridColumnHeader" without the curly brackets. Unfortunately, I have VS 2010 installed which comes standard with the DataGrid, and so cannot test using a namespace declaration.
All Time Programming 7-Sep-11 8:42am    
Nope that doesn't show the window only - the app just exits. Actually, I am able to access only my: DataGrid, DataGridCell, DataGridCellsPanel, DataGridHeaderBorder & DataGridRow - I tested in xaml to see if I can access DataGridColumnHeader or not !
Wayne Gaylard 7-Sep-11 9:18am    
To be honest, I find that very strange as you are able to access DataGridTextColumn et al in your xaml, and they are all part of the same control. I am at a loss, sorry mate.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900