jimbo
jimbo

Reputation: 621

DataGridRowTemplateColumn - refactoring and using styles efficiently?

I currently have a big lump of XAML that I am struggling to refactor.

<DataGrid x:Name="CurrentConfigDataGrid" ItemsSource="{Binding}"  >
    <DataGrid.Resources>
        <ResourceDictionary Source="../ResourceDictionaries/MergedDictionary.xaml" />
    </DataGrid.Resources>

    <DataGrid.ItemContainerStyle>
        <Style TargetType="{x:Type DataGridRow}" BasedOn="{StaticResource {x:Type DataGridRow}}" />
    </DataGrid.ItemContainerStyle>

    <DataGrid.Columns>            

        <DataGridCheckBoxColumn Width="25">                
        </DataGridCheckBoxColumn>

        <DataGridTemplateColumn Width="80" CanUserResize="False" CanUserSort="False" > 
            <DataGridTemplateColumn.Header>
                <Label Content="Type" />
            </DataGridTemplateColumn.Header>
            <DataGridTemplateColumn.CellTemplate>                    
                <DataTemplate>
                    <DataTemplate.Resources>
                        <Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}" />
                    </DataTemplate.Resources>
                    <TextBlock Text="{Binding Type}"  />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>                                
        </DataGridTemplateColumn>

        <DataGridTemplateColumn Width="150" CanUserResize="False" CanUserSort="False" >
            <DataGridTemplateColumn.Header>
                <Label Content="Version / Date" />
            </DataGridTemplateColumn.Header>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <DataTemplate.Resources>
                        <Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}" />
                    </DataTemplate.Resources>
                    <TextBlock Text="{Binding Version}"  />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

        <DataGridTemplateColumn Width="150" CanUserResize="False" CanUserSort="False" >
            <DataGridTemplateColumn.Header>
                <Label Content="GUID" />
            </DataGridTemplateColumn.Header>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <DataTemplate.Resources>
                        <Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}" />
                    </DataTemplate.Resources>
                    <TextBlock Text="{Binding GUID}"  />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

    </DataGrid.Columns>
</DataGrid>

How do I pull out the standard "TextBlock" style so it will be automatically picked up? I reference my MergedDictionary at the start, but this doesn't automatically convert the styles in the DataGrid to that of the textBlock unless I manually specify them... which means I need a DataGirdTemplateColumn, and then a Template, etc.

How is best to refactor?

Upvotes: 3

Views: 84

Answers (2)

almulo
almulo

Reputation: 4978

Does this work...?

<DataGrid x:Name="CurrentConfigDataGrid" ItemsSource="{Binding}"  >
    <DataGrid.Resources>
        <ResourceDictionary Source="../ResourceDictionaries/MergedDictionary.xaml" />
        <Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}" />
    </DataGrid.Resources>
...

Technically, if you create a Style in your Resources, with no Key but with a TargetType, it should be applied automatically to all controls of that type that have no explicit Style set.

        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Version}"  />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>

Upvotes: 2

Sinatr
Sinatr

Reputation: 22008

You have different headers and cell templates, I also had question of how to do selective templating. But what you doing wrong (as for me) is to re-define style in resources for each column.

Why not simply

<DataGridTemplateColumn Header="Type"> 
    <DataGridTemplateColumn.CellTemplate>                    
        <DataTemplate>
            <TextBlock Text="{Binding Type}"
                       Style={StaticResource TextBlock} />
        </DataTemplate>
    </DataGridTemplateColumn>

You still need to merge dictionary, containing TextBlock style, preferably on highest level (Window or UserControl resources).

Upvotes: 0

Related Questions