Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
DataGridColumnHeader数据模板中的WPF绑定_Wpf_Templates_Xaml_Data Binding_Datagrid - Fatal编程技术网

DataGridColumnHeader数据模板中的WPF绑定

DataGridColumnHeader数据模板中的WPF绑定,wpf,templates,xaml,data-binding,datagrid,Wpf,Templates,Xaml,Data Binding,Datagrid,因此,这是对以下问题的延伸: 简而言之,我试图通过使用组合框模板化列标题,将过滤器放入我的DataGridColumnHeaders中。因此,与另一个示例的不同之处在于,我使用的是组合框 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.

因此,这是对以下问题的延伸:

简而言之,我试图通过使用组合框模板化列标题,将过滤器放入我的DataGridColumnHeaders中。因此,与另一个示例的不同之处在于,我使用的是组合框

<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>


我的问题涉及将组合框绑定到一些值。如上图所示,我目前在将ItemsSource绑定到ViewModel中的属性时遇到问题,但我无法使其正常工作。我的第二个问题是如何修改代码,以便可以为每列绑定不同的值???

DataGridColumnHeaders不继承
DataContext,因此它们没有任何绑定依据。使用
RelativeSource
在绑定中查找父级
DataGrid
,并指向
DataContext.code

<DataTemplate x:Key="MySpecialHeaderTemplate">
    <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
                                    Path=DataContext.Codes}" />
</DataTemplate>

DataGridColumnHeader
不继承
DataContext
,因此它们没有任何可绑定的内容。使用
RelativeSource
在绑定中查找父级
DataGrid
,并指向
DataContext.code

<DataTemplate x:Key="MySpecialHeaderTemplate">
    <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
                                    Path=DataContext.Codes}" />
</DataTemplate>


输出窗口中有哪些绑定错误?在哪里设置DataContext?输出窗口中有哪些绑定错误?在哪里设置数据上下文?谢谢!这解决了我问题的第一部分。您能帮助绑定到我的datagrid中每列的不同集合吗?我现在正在玩…谢谢。对于我的第二个问题,我认为我必须为每列定义headertemplate,而不是使用共享资源。谢谢!这解决了我问题的第一部分。您能帮助绑定到我的datagrid中每列的不同集合吗?我现在正在玩…谢谢。对于第二个问题,我认为我必须为每列定义headertemplate,而不是使用共享资源。