1


I've several DataGrid in my application and they each have the same "template". For example, here's how each DataGrid is defined:

<DataGrid Style="{StaticResource MainGridStyle}">
    <DataGrid.Columns>
    <DataGridTemplateColumn CanUserResize="False"
        CanUserSort="False"
            CanUserReorder="False"
            CellStyle="{StaticResource RightCellStyle}">
...

How could I define the "DataGridTemplateColumn" as a Template in an external resource file, so I would simply have to write something like

<DataGridTemplateColumn Style={StaticResource MyFirstColumn}/>

In the "MainGridStyle" I define properties such "CanUserAddRows", ...

Thx in advance for your help.
Fred

Fred
  • 596
  • 1
  • 6
  • 19

3 Answers3

4

You're talking about 4 different things here:

  • the dataGrid's style
  • the DataGrid's template
  • your first column's style
  • your first column's template

so let's get precise first: chose one and stick to it, try not not mix style and template (one can contain the other anyway)

now from what I understand, you're more interested in making a template for your first column than for the whole dataGrid.

It should be pretty easy:

1) first, declare your column's template (or style) in a resource dictionary (preferably in your application's resources):

<Application.Resources>
    <Template TargetType="DataGridTemplateColumn" x:Key="MyFirstColumnTemplate ">
    ...
    </Template>
</Application.Resources>

2) then, simply call it like you wanted to do:

<DataGrid Style="{StaticResource MainGridStyle}">
    <DataGrid.Columns>
        <DataGridTemplateColumn Template="{StaticResource MyFirstColumnTemplate}"/>
        ...
    </DataGrid.Columns>
<DataGrid>

EDIT:

in the case of a dataGridTemplateColumn, as you only have the CellTemplate and CellEditingTemplate properties available, you can do as follow:

<Application.Resources>
     <DataTemplate x:Key="CellTemplate">
     ...
     </DataTemplate>
     <DataTemplate x:Key="CellEdintingTemplate">
     ...
     </DataTemplate>
</Application.Resources>

<DataGrid Style="{StaticResource MainGridStyle}">
    <DataGrid.Columns>
        <DataGridTemplateColumn CellTemplate="{StaticResource MyFirstColumnCellTemplate}" CellEdintingTemplate="{StaticResource MyFirstColumnCellEdintingTemplate}"/>
        ...
    </DataGrid.Columns>
<DataGrid>

disclaimer : I'm not sure if it's a controlTemplate or a dataTemplate for the cell(Editing)Template, try both and see wich one fits

David
  • 6,014
  • 4
  • 39
  • 55
  • Well that sounds quite trivial. And if I want to declare this "Template" in an external resource file/dictionary? By the way, my VisualStudio complains that there's no Template property in DataGridTemplateColumn! – Fred Nov 23 '10 at 08:55
  • indeed, for a dataGridTempalteColumn, you only have the CellTemplate and CellEditingTemplate properties available. have a look there: http://msdn.microsoft.com/fr-fr/library/system.windows.controls.datagridtemplatecolumn_properties%28v=VS.95%29.aspx – David Nov 23 '10 at 09:01
  • For the CellTemplate, it was clear to me. But for DataGridTemplateColumn, that's something else. So in fact, it seems impossible to define a "custom reusable column template"?!?! Strange! – Fred Nov 23 '10 at 09:10
  • hmmm no, I do not see you problem here. You can simply make your 2 templates and link those properties instead of juste the one "template" property? – David Nov 23 '10 at 09:13
  • It doesn't work either. The Element "DataGridTemplateColumn" does not have a Property called "Style"!!! So, I think, I'm stucked! That's the (main) problem! – Fred Nov 23 '10 at 09:35
1

Since DataGridTemplateColumn does not have a Style property, one thing you can do is create an attached property.

Following is an example :

[NOTE: You may need to change the following code to suit you project.]

Class with an attached property -

public class StyleExtensions
{
    public static Style GetStyle(DependencyObject obj)
    {
        return (Style)obj.GetValue(StyleProperty);
    }

    public static void SetStyle(DependencyObject obj, Style value)
    {
        obj.SetValue(StyleProperty, value);
    }

    public static void StyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Style style = e.NewValue as Style;
        if (style != null)
        {
            foreach (var s in style.Setters.OfType<Setter>())
            {
                d.SetValue(s.Property, s.Value);
            }
        }
    }

    // Using a DependencyProperty as the backing store for Style.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty StyleProperty =
        DependencyProperty.RegisterAttached("Style", typeof(Style), typeof(StyleExtensions), new UIPropertyMetadata(StyleChanged));
}

Definition of Style -

    <Style x:Key="MyFirstColumn">
        <Setter Property="DataGridColumn.CanUserResize"
                Value="False" />
        <Setter Property="DataGridColumn.CanUserSort"
                Value="False" />
        <Setter Property="DataGridColumn.CanUserReorder"
                Value="False" />
        <Setter Property="DataGridColumn.CellStyle"
                Value="{StaticResource RightCellStyle}" />
    </Style>

Use -

            <DataGrid>
                <DataGrid.Columns>
                    <DataGridTemplateColumn local:StyleExtensions.Style="{StaticResource MyFirstColumn}"></DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
decyclone
  • 30,394
  • 6
  • 63
  • 80
0

For the "external" resource file, you can simply put it in a resource dictionary that you call in you app.xaml file:

in your "customTemplates.xaml" file :

<ResourceDictionary>
...
</ResourceDictionary>

and then in you "app.xaml" file:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="customTemplates.xaml" />
            ...
            //add other resource dictionaries here if any
            ...
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
David
  • 6,014
  • 4
  • 39
  • 55