Wpf Toolkit. Bind DataGrid Column Header to DynamicResource
Asked Answered
P

4

7

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.

Pneumatics answered 16/3, 2009 at 21:32 Comment(0)
L
9

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.

Libertylibia answered 9/7, 2009 at 5:31 Comment(0)
B
9

this works for me with internationalization using dictionaries

<DataGrid>
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{x:Null}" Width="120">
            <DataGridTextColumn.Header>
                <TextBlock Text="{DynamicResource gridColumnHeader1}" />
            </DataGridTextColumn.Header>
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>
Briquette answered 9/11, 2014 at 20:43 Comment(1)
This is the correct answer if you need a DynamicResource and can't use a StaticResource.Adp
P
2

If you just want to change the Header and dont' want to bother with styles, do this. (credit goes to above poster)

    <Window.Resources>
    <sys:String x:Key="HeaderText">Header Text</sys:String>
    <TextBlock x:Key="HeaderSR" Text="{DynamicResource HeaderText}"/>
</Window.Resources>
<Grid>
    <tk:DataGrid>
        <tk:DataGrid.Columns>
            <tk:DataGridTextColumn Header="{StaticResource HeaderSR}" Width="100"/>
        </tk:DataGrid.Columns>
    </tk:DataGrid>
</Grid>
Playground answered 27/4, 2010 at 11:56 Comment(0)
N
1

Try this...

<DataGrid>
   <DataGrid.Columns>
       <DataGridTextColumn    Binding="{Binding InputDate,Mode=OneWay,StringFormat='{}{0:dd MMM yy}'}" Width="70" >
          <DataGridTextColumn.Header>
              <TextBlock Text="{DynamicResource ResourceKey=inputDate}" />
           </DataGridTextColumn.Header>
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>
Newsom answered 17/3, 2013 at 2:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.