Wpf Toolkit. Bind DataGrid Column Header to DynamicResource

纵饮孤独 提交于 2019-11-27 22:31:24

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.

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>

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>

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!