找到了一个有趣的解决方法,它也适用于wpflocalizeaddin.codeplex.com:
由Slyi创建
它使用一个IValueConverter
:
public class BindingConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value.GetType().Name == "Binding")
{
ContentControl cc = new ContentControl();
cc.SetBinding(ContentControl.ContentProperty, value as Binding);
return cc;
}
else return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
还有一种风格DataGridColumnHeader
<UserControl.Resources>
<local:BindingConverter x:Key="BindCon"/>
<Style x:Key="ColBinding" TargetType="dataprimitives:DataGridColumnHeader" >
<Setter Property="ContentTemplate" >
<Setter.Value>
<DataTemplate>
<ContentPresenter Content="{Binding Converter={StaticResource BindCon}}" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
这样您就可以在Header
属性上保留您最喜欢的绑定语法
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel>
<TextBox Text="binding header" x:Name="tbox" />
<data:DataGrid ItemsSource="{Binding AllPeople,Source={StaticResource folks}}" AutoGenerateColumns="False" ColumnHeaderStyle="{StaticResource ColBinding}" >
<data:DataGrid.Columns>
<data:DataGridTextColumn Binding="{Binding ID}"
Header="{Binding Text, ElementName=tbox}" />
<data:DataGridTextColumn Binding="{Binding Name}"
Header="hello" />
</data:DataGrid.Columns>
</data:DataGrid>
</StackPanel>
</Grid>
http://cid-289eaf995528b9fd.skydrive.live.com/self.aspx/Public/HeaderBinding.zip