DataGridRowTemplateColumn - efficiently refactoring and styling?
I currently have a large chunk of XAML that I am trying 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 default "TextBlock" style so that it auto-picks up? I reference my MergedDictionary first, but this does not automatically convert the styles in the DataGrid to TextBlock styles unless I manually define them ... which means I need the DataGirdTemplateColumn and then the template, etc.
What's the best way to reorganize?
source to share
It works...?
<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 without a key, but with a TargetType, it should be applied automatically to all controls of that type that do not have an explicit style set.
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Version}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
source to share
You have different headers and cell templates, I also had a question on how to make custom templates. But what you are doing wrong (as for me) is overriding the style in resources for each column.
Why not just
<DataGridTemplateColumn Header="Type">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Type}"
Style={StaticResource TextBlock} />
</DataTemplate>
</DataGridTemplateColumn>
You still need to merge the vocabulary containing the style TextBlock
, preferably at the highest level ( Window
or UserControl
).
source to share