WPF:将值绑定到GridViewColumn内的组合框时出现问题

WPF:将值绑定到GridViewColumn内的组合框时出现问题,wpf,data-binding,combobox,gridviewcolumn,Wpf,Data Binding,Combobox,Gridviewcolumn,My Views dataContext绑定到一个具有两个observableCollections成员的presentationModel。在视图中,我有一个listView,ItemSource绑定到该listView是第一个observableCollection。在其中一个LilstViews列中,我想显示presentationModel中第二个Observable集合的值。我不知道如何将observableCollection中的值放入我的组合框。有人知道如何解决这个问题吗?您需要做

My Views dataContext绑定到一个具有两个observableCollections成员的presentationModel。在视图中,我有一个listView,ItemSource绑定到该listView是第一个observableCollection。在其中一个LilstViews列中,我想显示presentationModel中第二个Observable集合的值。我不知道如何将observableCollection中的值放入我的组合框。有人知道如何解决这个问题吗?

您需要做的第一件事是创建一个包含组合框的数据模板,在这种情况下,我已将ItemsSource绑定到主机窗口上的DependencyProperty。其中包含表示模型,该模型具有名为ComboSource的属性。SelectedValue已通过ListViewItem的DataContext绑定到保存选定值的属性

<ListView.Resources>
    <DataTemplate x:Key="comboBoxTemplate">
        <ComboBox
            ItemsSource="{Binding 
                            Path=ModelData.ComboSource, 
                            RelativeSource={RelativeSource AncestorType=Window}}"
            SelectedValue="{Binding 
                            Path=DataContext.Selection, 
                            RelativeSource={RelativeSource AncestorType=ListViewItem}}"
            DisplayMemberPath="Item"
            SelectedValuePath="Id"
            />
    </DataTemplate>
</ListView.Resources>

然后您需要从GridViewColumn上的CellTemplate中引用它

<GridViewColumn
    Header="Selection"
    Width="160"
    CellTemplate="{StaticResource comboBoxTemplate}"
    />