Why can't I use DynamicResource with DataGridColumn.CellStyle Why can't I use DynamicResource with DataGridColumn.CellStyle wpf wpf

Why can't I use DynamicResource with DataGridColumn.CellStyle


You could create resources for the properties in your DataGridCell Style and then reference them as a DynamicResource within the Style definition:

Based on your example it would look like this:

<Window.Resources>    <SolidColorBrush x:Key="ForegroundBrush" Color="Blue"/>    <Style x:Key="BaseCellClass" TargetType="DataGridCell">        <Setter Property="Foreground" Value="{DynamicResource ForegroundBrush}" />    </Style></Window.Resources><Grid>    <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding ElementName=WholeWindow, Path=ViewModel.Objects}">        <DataGrid.Columns>            <DataGridTextColumn Binding="{Binding F1}" Header="F1" CellStyle="{StaticResource BaseCellClass}" />            <DataGridTextColumn Binding="{Binding F2}" Header="F2" CellStyle="{StaticResource BaseCellClass}" />        </DataGrid.Columns>    </DataGrid></Grid>

The resources would of course be located in separate resource files.


I found solution with using a little service. In few words I write in xaml this code:

<wpfApplication12:DataGridColumnDynamicStyleService TargetGrid="{Binding ElementName=Grid}">        <wpfApplication12:DataGridColumnDynamicStyleService.ColumnStyles>            <wpfApplication12:DataGridColumnStyleBinding ColumnTag="C1" DynamicStyle="{DynamicResource BaseCellClass}" />        </wpfApplication12:DataGridColumnDynamicStyleService.ColumnStyles>    </wpfApplication12:DataGridColumnDynamicStyleService>    <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding ElementName=WholeWindow, Path=ViewModel.Objects}" x:Name="Grid">        <DataGrid.Columns>            <DataGridTextColumn Binding="{Binding F1}" Header="F1" wpfApplication12:DataGridColumnDynamicStyle.ColumnTag="C1" />            <DataGridTextColumn Binding="{Binding F2}" Header="F2" wpfApplication12:DataGridColumnDynamicStyle.ColumnTag="C2" />        </DataGrid.Columns>    </DataGrid>

Here, as you can see, I use attached property ColumnTag to identify columns. And I create a service control that defines styles for columns and set target datagrid as TargetGridIf you want to see all the code, here is the link to the solution on google drive