因此,这是对以下问题的扩展:Style DataGridColumnHeader with Styles in WPF
简而言之,我试图通过使用组合框模板列标题来将过滤器放入我的DataGridColumnHeaders中。因此,与其他示例的不同之处在于,我使用的是ComboBoxes。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="300" Loaded="Window_Loaded">
<Window.Resources>
<DataTemplate x:Key="MySpecialHeaderTemplate">
<ComboBox ItemsSource="{Binding Codes}" />
</DataTemplate>
</Window.Resources>
<Grid>
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn
Binding="{Binding Id}" />
<DataGridTextColumn HeaderTemplate="{StaticResource MySpecialHeaderTemplate}"
Binding="{Binding Name}" />
<DataGridTextColumn HeaderTemplate="{StaticResource MySpecialHeaderTemplate}"
Binding="{Binding Age}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
我的问题与将ComboBox绑定到某些值有关。我目前在将ItemsSource绑定到我的ViewModel中的一个属性时遇到了上述问题,但我无法让它正常工作。我的第二个问题是如何修改代码,以便可以绑定到每列的不同值?
发布于 2011-08-17 10:41:41
DataGridColumnHeaders不继承DataContext,因此它们没有什么可绑定的。改用RelativeSource查找绑定中的父DataGrid并指向DataContext.Codes
<DataTemplate x:Key="MySpecialHeaderTemplate">
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
Path=DataContext.Codes}" />
</DataTemplate>https://stackoverflow.com/questions/7087299
复制相似问题