Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 444
jimbo Avatar asked May 07 '26 16:05

jimbo


1 Answers

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>
like image 134
almulo Avatar answered May 09 '26 04:05

almulo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!