Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.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
Wpf DataGrid TemplateClunn组合框项资源和SelectedValue_Wpf_Mvvm_Combobox_Datagridtemplatecolumn - Fatal编程技术网

Wpf DataGrid TemplateClunn组合框项资源和SelectedValue

Wpf DataGrid TemplateClunn组合框项资源和SelectedValue,wpf,mvvm,combobox,datagridtemplatecolumn,Wpf,Mvvm,Combobox,Datagridtemplatecolumn,嗨,我有一个有两列的数据网格。如下所示: <DataGrid ItemsSource="{Binding MyCollection}"> <DataGrid.Columns> <DataGridTextColumn Width="85*" Header="Team Name" Binding="{Binding TeamName}"/> <DataGridTemplateColumn Header="Prefix"> &

嗨,我有一个有两列的数据网格。如下所示:

<DataGrid ItemsSource="{Binding MyCollection}">
<DataGrid.Columns>
   <DataGridTextColumn Width="85*" Header="Team Name" Binding="{Binding TeamName}"/>
   <DataGridTemplateColumn Header="Prefix">
        <DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
                 <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.AllowedPrefixes}" 
                           SelectedValue="{Binding Prefix}"> 
//SelectedValue="{Binding Prefix}" - this is not working i also tried SelectedItem

                </ComboBox>
             </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
   </DataGridTemplateColumn>
</DataGrid.Columns>
正如您所看到的,我有一个带有组合框的DataGridTemplateColumn。我已将ComboBox ItemsSource绑定到一个包含一些固定值的集合。现在我想将组合框的SelectedValue绑定到MyClass的Prefix属性这是我的DataGrid的DataContext。但由于我将这个组合框的ItemsSource设置为其他集合,所以这个绑定不起作用,我认为datacontext已经改变了。 下面是一个类,它是datagrid的datacontext:

public class MyClass
{
    public string TeamName{get;set;}
    public string Prefix{get;set;}
}
// DataGrid.DataContext is ObservableCollection<MyClass>
因此,AllowedPrefixes集合在ComboBox中正确显示,但Prefix属性未被更新。如何正确绑定此SelectedValue

编辑

请注意,ComboBox ItemsSource与我要用SelectedValue更新的集合不同

编辑

我认为它不起作用,因为我将ComboBox ItemsSource设置为不同的集合。我尝试了以下方法,但没有成功:

<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.AllowedPrefixes}" 
                                      SelectedValue="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridTextColumn}}, Path=DataContext.Prefix, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">

为您的组合框尝试以下操作:


如果您的DataGrid位于UserControl而不是Windows中,请将AncestorType更改为UserControl

好吧,尽管您不知道答案,但他们没有解释就否决了我。我找到了解决办法。我将它发布在下面,将来可能有人会使用它,我必须将NotifyOnTargetUpdated设置为true:

<ComboBox 
    SelectedItem="{Binding Prefix, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"
    ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.AllowedPrefixes}">

请添加错误或说明它是如何工作的您可能想使用SelectedItem,而不是SelectedValue-没有错误,只是没有更新viemodels prefix属性。所选项目不会更改任何内容。继续向下投票-继续向下投票而不是帮助。甚至没有解释…添加Mode=TwoWay到绑定中,它可能是默认的单向方式。我想你误解了我的意思。我的Datagrid itemssource是ObservableCollection,其中MyClass包含属性前缀。因此,我的用户控件DataContext没有属性前缀
<ComboBox 
    SelectedItem="{Binding Prefix, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"
    ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.AllowedPrefixes}">