Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.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
C# WPF组合框主-从_C#_Wpf_Mvvm_Combobox - Fatal编程技术网

C# WPF组合框主-从

C# WPF组合框主-从,c#,wpf,mvvm,combobox,C#,Wpf,Mvvm,Combobox,我有两个combobox主从式: <ComboBox ItemSource="{Binding MySource}" SelectedItem="{Binding MySelectedItem}" DisplayMemberPath="Description" /> <ComboBox ItemSource="{Binding MySelectedItem.Items}" IsSynchronizedWithCurrentItem="{x:Null}" /> 但当我

我有两个combobox主从式:

<ComboBox ItemSource="{Binding MySource}" SelectedItem="{Binding MySelectedItem}" DisplayMemberPath="Description" />
<ComboBox ItemSource="{Binding MySelectedItem.Items}" IsSynchronizedWithCurrentItem="{x:Null}" />

但当我选择第一个组合框中的一个项目时,在我选择了其中一个项目和第二个组合框中的选定项目后,它的项目列表为空。第二个组合框中的文本不是空的。我也尝试过使用IsSynchronizedWithCurrentItem=“False”


有什么问题吗?

我不明白你想说什么。。。但我很确定,在修改“MySelectedItem”后,您没有通知属性更改,并且忘记了mode=TwoWay

如果要在ViewModel中使用SelectedItem:

private YourItemType _mySelectedItem;
public YourItemType MySelectedItem
{
 get { return (_mySelectedItem);}
set
{
if (_mySelectedItem != value)
{
 _mySelectedItem = value;
if (PropertyChanged != null)
   PropertyChanged(this, new PropertyChangedEventArgs("MySelectedItem"));
}
}
Xaml:

}

如果您只想进行筛选,请执行以下操作:

<ComboBox ItemSource="{Binding MySource}" DisplayMemberPath="Description" name="source"/>
<ComboBox ItemSource="{Binding SelectedItem.Items, ElementName=source}"/>


我发现了问题,原因是我在组合框中将一个命令与交互性相关联,这捕获了一个异常,并且没有继续执行。

您能重新表述您想要的内容吗?我不太理解这个问题。是的,SelectedItem正在通知propertychanged,我尝试了Mode=Twoway,但问题仍然存在。我的步骤是:在combo1中选择项目A,在combo2中选择项目A1,然后在combo1中选择项目B。然后combo2为空,因为项B没有子项,但combo2在其文本中显示itemA1尝试在第二个组合中绑定selectedIndex,并在items.count==0时将其设置为-1
<ComboBox ItemSource="{Binding MySource}" DisplayMemberPath="Description" name="source"/>
<ComboBox ItemSource="{Binding SelectedItem.Items, ElementName=source}"/>