question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DynamicResource is not working for Header property in DataGridTextColumn

See original GitHub issue
  • .NET Core Version: .Net 6.0
  • Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes

Problem description:

I have defined the DynamicResource for Header property in DataGridTextColumn of DataGrid. Please refer the below code snippet,

<Application.Resources>
        <System:String x:Key="firstName"  >Before Change Name</System:String>
        <System:String x:Key="secondName" >After Change Name</System:String>
    </Application.Resources>
<DataGridTextColumn Binding="{Binding CustomerID}" 
                                    Header="{DynamicResource firstName}" />

When change the resource at runtime Header property does not change the value in DataGrid. Can you please check and elaborate why DynamicResource is not working in DataGridTextColumn.Header property of DataGrid?

Note : If we use HeaderTemplate property in DataGridTextColumn its working properly in DataGrid. Please refer the code snippet,

<Window.Resources>
        <DataTemplate x:Key="headerTemplate">
            <TextBlock Height="50"                    
                    Text="{DynamicResource firstName}"
                    TextWrapping="Wrap" />
        </DataTemplate>
</Window.Resources>   

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="200" />
        </Grid.ColumnDefinitions>
        <DataGrid x:Name="dataGrid"                                 
                  ItemsSource="{Binding Orders}"
                  AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding OrderID}" 
                                    HeaderTemplate="{StaticResource headerTemplate}" />
                <DataGridTextColumn Binding="{Binding CustomerID}" 
                                    Header="{DynamicResource firstName}" />
                <DataGridTextColumn Binding="{Binding CustomerName}" Header="Customer Name" />
                <DataGridTextColumn Binding="{Binding Country}" Header="Country" />
                <DataGridTextColumn Binding="{Binding UnitPrice}" Header="Unit Price" />
            </DataGrid.Columns>
        </DataGrid>
</Grid>

Please refer the Screenshot for your reference Resource

Minimal repro: Step 1: Run the sample

Step 2: Click the Change resource button

Actual behavior: DynamicResource is not working for Header property in DataGridTextColumn

Expected behavior: DynamicResource is working properly for Header property in DataGridTextColumn

I have attached the sample for your reference. Sample Link: DataGrid.zip

Can you please check and provide the solution to use the DynamicResource in Header property in DataGridTextColumn?

Regards, Vijayarasan S

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
anjali-wpfcommented, Apr 3, 2022

@vijayarasan

For this one you are mixing two things together, you have to pick either of the ones mentioned below.

Either use

<Window.Resources>
        <DataTemplate x:Key="headerTemplate">
            <TextBlock Height="50"                    
                    Text="{DynamicResource firstName}"
                    TextWrapping="Wrap" />
        </DataTemplate>
    </Window.Resources>  

and

<DataGridTextColumn Binding="{Binding OrderID}" 
                                    HeaderTemplate="{StaticResource headerTemplate}" />

Or use

<Window.Resources>
        <TextBlock x:Key="TextBlockName" Height="50"                    
                    Text="{DynamicResource firstName}"
                    TextWrapping="Wrap" />
    </Window.Resources> 

and

<DataGridTextColumn Binding="{Binding OrderID}" 
                                    Header="{StaticResource TextBlockName}" />

In this first approach we are setting style with the help of header template, and in the second one we are setting it with the help of static resource.

I hope this addresses your question, in such a case we can close it.

0reactions
vijayarasancommented, Oct 18, 2022

Hi @miloush,

The purpose of requesting the simple way to access the property that is bound for HeaderText is a very simple comparatively process with HeaderTemplate

Read more comments on GitHub >

github_iconTop Results From Across the Web

Wpf Toolkit. Bind DataGrid Column Header to ...
But for some strange reason column header remains empty. StaticResource however works well. Could you please help me to figure out how to...
Read more >
DataGridColumn.HeaderStyle Property (System.Windows. ...
Gets or sets the style that is used when rendering the column header. ... HeaderStyle property, which takes precedence over the DataGrid.
Read more >
DataGridTextColumn Class (System.Windows.Controls)
Represents a DataGrid column that hosts textual content in its cells. ... can customize the DataGridTextColumn by setting properties, such as Width, Header, ......
Read more >
Vault 2024 Help | Dynamic Properties
Dynamic Properties allow you to show and edit properties that are assigned to a Vault category without the need to modify the XAML...
Read more >
WPF XAML Designer Intellisense Feature Not Working
I tried to investigate my XAML code one by one and found an issue with using MultiBinding on DataGridTextColum. <DataGridTextColumn Header="Date ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found