Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wpf Toolkit. Bind DataGrid Column Header to DynamicResource

I'm trying to bind DataColumn Header to DynamicResource using following code.

<Window.Resources>
    <sys:String x:Key="HeaderText">Header Text</sys:String>
</Window.Resources>
<Grid>
    <tk:DataGrid>
        <tk:DataGrid.Columns>
            <tk:DataGridTextColumn Header="{DynamicResource HeaderText}" Width="100"/>
        </tk:DataGrid.Columns>
    </tk:DataGrid>
</Grid>

But for some strange reason column header remains empty. StaticResource however works well. Could you please help me to figure out how to bind that Header property to some DynamicResource.

like image 273
Oleg Avatar asked Mar 16 '09 21:03

Oleg


1 Answers

Try this:

<Window.Resources>
    <sys:String x:Key="HeaderText">Header Text</sys:String>
    <Style x:Key="HeaderTextStyle" TargetType="{x:Type Primitives:DataGridColumnHeader}">
       <Setter Property="Content" Value="{DynamicResource HeaderText}" />
    </Style>
</Window.Resources>
<Grid>
    <tk:DataGrid>
        <tk:DataGrid.Columns>
            <tk:DataGridTextColumn HeaderStyle="{StaticResource HeaderTextStyle}" Width="100"/>
        </tk:DataGrid.Columns>
    </tk:DataGrid>
</Grid>

WPF Toolkit's DataGrid has DataGridColumns which are not Visual controls, so they have some funny rules. One of those funny rules is that only the Binding property can be a Binding - everything else must be static. To circumvent this, you can create a Static Style Resource which contains Dynamic Content.

like image 149
vanja. Avatar answered Sep 22 '22 13:09

vanja.