将 ComboBox 框项目绑定到另一个 Combobox 的 SelectedItem
我有两个组合框。第一个,Companys 有一个 Company 对象的集合。每个对象都有一个字符串名称和字符串集合:
这是类:
public class clsCompany : PropertyChangedBase
{
public string Name { get; set; }
public BindableCollection<string> Regions;
public override string ToString()
{
return Name;
}
}
这是代码。当我选择一个项目时,文本框更新得很好。但是应该显示区域的 ComboBox 却没有:
<ComboBox x:Name="Companys" DisplayMemberPath="Name" Width="100"/>
<ComboBox x:Name="SelectedCompany_Regions" Width="100"/>
<TextBlock x:Name="SelectedCompany_Name" Width="100" />
我能让第二个 ComboBox 工作的唯一方法是在我的 ViewModel 上创建一个与 SelectedCompany.Regions 关联的新属性但是为什么我没有工作?
I have two combo boxes. The first one, Companys has a collection of Company objects. Each object has a string Name and string collection:
Here is the class:
public class clsCompany : PropertyChangedBase
{
public string Name { get; set; }
public BindableCollection<string> Regions;
public override string ToString()
{
return Name;
}
}
Here is the code. The TextBox updates just fine when I select an item. But the ComboBox which should show the regions does not:
<ComboBox x:Name="Companys" DisplayMemberPath="Name" Width="100"/>
<ComboBox x:Name="SelectedCompany_Regions" Width="100"/>
<TextBlock x:Name="SelectedCompany_Name" Width="100" />
The only way I can get the 2nd ComboBox to work is to create a new property on my ViewModel which is associated with SelectedCompany.Regions But why does what I have not work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题是由于缺少属性更改通知引起的。当您更新公司时,您还需要为该公司区域发出属性更改通知,以便区域 ComboBox 知道其源也已更新。您可以通过将 Companies 和 SelectedCompany 属性更改为以下内容来实现此目的:
Your issue is caused by missing property changed notifications. When you update the Company you also need to raise a property changed notification for that companies Regions so that the regions
ComboBox
is aware its source has also updated. You can achieve this by changing the Companies and SelectedCompany properties to the following:如果您将 Companys 设置为 CollectionView,那么您应该能够将第二个组合绑定到 Companys.CurrentItem.Regions。
If you make Companys a CollectionView then you should be able to bind the second combo to Companys.CurrentItem.Regions.