0

In my application I have a Window with three DataGrids on it. All DataGrids should have the same colums.

Two of the DataGrids are bound to an ObservableCollection<DisplayItem>. The third DataGrid is bound to an ObservableCollection<GeneralItem>.

The class GeneralItem has a property of type DisplayItem.

The definition of the DataGridTemplateColumn for the Item-Name in two of three DataGrids looks like:

<DataGridTemplateColumn Width="Auto" Header="Item-Name">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
        <TextBlock Style="{StaticResource DataGridTextBlockStyle}" 
                   Text="{Binding Module.DisplayItem.Text}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

The definition of the DataGridTemplateColumn for the Item-Name in the third DataGrid looks like:

<DataGridTemplateColumn Width="Auto" Header="Item-Name">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
        <TextBlock Style="{StaticResource DataGridTextBlockStyle}" 
                   Text="{Binding Module.GeneralItem.DisplayItem.Text}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

I know that I can make DataGridTemplateColumns to resources and use it as StaticResources.

What I now want to know: Is there a possibility that I only have to write the DataGridTemplateColumn once and it can handle both situations? For example by providing a DataContext to the StaticResource or so.

1 Answer 1

1

You can create a template selector to allow you to choose which template to use.

<Window.Resource>
    <DataTemplate x:Key="FirstTemplate">
        <TextBlock Style="{StaticResource DataGridTextBlockStyle}" 
                   Text="{Binding Module.DisplayItem.Text}"/>
    </DataTemplate>
    <DataTemplate x:Key="SecondTemplate">
        <TextBlock Style="{StaticResource DataGridTextBlockStyle}" 
                   Text="{Binding Module.GeneralItem.DisplayItem.Text}"/>
    </DataTemplate>
</Window.Resource>
<DataGridTemplateColumn Header="Item-Name">
    <DataGridTemplateColumn.CellTemplateSelector>
        <local:DisplayItemTemplateSelector
            FirstTemplate="{StaticResource FirstTemplate}"
            SecondTemplate="{StaticResource SecondTemplate}"/>
    </DataGridTemplateColumn.CellTemplateSelector>
</DataGridTemplateColumn>

public class DisplayItemTemplateSelector : DataTemplateSelector
{
  public DataTemplate FirstTemplate
  { get; set; }

  public DataTemplate SecondTemplate
  { get; set; }

  public override DataTemplate SelectTemplate(object item, DependencyObject container)
  {

    if (item is  GeneralItem)
    {
        //second template etc
    }
    else if (item is DisplayItem)
    {
        //first template
    }
    else
      return base.SelectTemplate(item, container);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.