gpt4 book ai didi

c# - 让 ListBox 中的项目取决于在 ComboBox 中选择的项目

转载 作者:行者123 更新时间:2023-11-30 17:42:43 24 4
gpt4 key购买 nike

我有一个带有 ComboBox 的小型 WPF 应用程序,用户可以在其中从项目列表中进行选择。我还有一个 ListBox,我希望 ListBox 中的可用项目取决于当前在 ComboBox 中选择的项目。

假设 ComboBox 有以下选项:“Fruits”和“Vegetables”。如果我选择“Fruits”,ListBox 将包含“Apple”、“Banana”、“Pear”等,如果我选择“Vegetables”,它将包含“Carrot”、“Potato”等。

这只是一个虚构的例子,但涵盖了我所需要的。在我的应用程序中 - ComboBox 的数据和要放入 ListBox 的任何内容都将来自外部数据源。

我该怎么做?我已经将我的 View 模型绑定(bind)到 View 并从数据源填充了 ComboBox,但是我需要 ListBox 的内容来反射(reflect) ComboBox 中的选定选项。

最佳答案

创建 2 个列表并根据选择将其中一个绑定(bind)到您的列表框。例如:

List <string> Fruits=new List<string>(){"apple","banana",..};
List <string> Vegetables=new List<string>(){"tomato","Potato",..};

并且在您的组合框选择更改事件中:

private void OComboBox1Changed(object sender, SelectionChangedEventArgs e)
{
if (ComboBox1.Selected.Item=...)
{
listbox1.ItemsSource=Fruits;
}
else
{
listbox1.ItemsSource=Vegetables;
}
}

关于c# - 让 ListBox 中的项目取决于在 ComboBox 中选择的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31698558/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com