动态更改 WPF ComboBox 的 ItemsSource
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20996611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamically changing ItemsSource of a WPF ComboBox
提问by bmt22033
I have a WPF application that contains two comboboxes (we'll call them cbox1 and cbox2). cbox1 has its ItemsSource bound to an enum via XAML like this:
我有一个 WPF 应用程序,其中包含两个组合框(我们将它们称为 cbox1 和 cbox2)。cbox1 将其 ItemsSource 通过 XAML 绑定到枚举,如下所示:
<Window.Resources>
<local:EnumDescriptionConverter x:Key="enumDescriptionConverter"/>
<ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="cbox1DataProvider">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:MyModel+ModeOfTransportationEnum"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<ComboBox x:Name="cbox1" ItemsSource="{Binding Source={StaticResource cbox1DataProvider}}" SelectionChanged="cbox1_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource enumDescriptionConverter}}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
The enum that cbox1's ItemsSource is bound to looks like this:
cbox1 的 ItemsSource 绑定的枚举如下所示:
public enum ModeOfTransportationEnum
{
[Description("BMW X5")]
BmwX5,
[Description("Toyota Camry")]
ToyotaCamry,
[Description("Ford Focus")]
FordFocus
}
When my user selects an item in cbox1, I want to dynamically define the ItemsSource for cbox2. For example, if my user selects "Toyota Camry" from cbox1, I want cbox2 to display the values "Red" and "Black". If the user chooses "Ford Focus" from cbox1, I might want cbox2 to display "Silver", "Red" and "Blue".
当我的用户在 cbox1 中选择一个项目时,我想为 cbox2 动态定义 ItemsSource。例如,如果我的用户从 cbox1 中选择“Toyota Camry”,我希望 cbox2 显示值“Red”和“Black”。如果用户从 cbox1 中选择“Ford Focus”,我可能希望 cbox2 显示“Silver”、“Red”和“Blue”。
I've probably over-simplified the example but in a nutshell, I have three enums that I want to use for the .ItemsSource binding of cbox2. I want to set the appropriate enum as the .ItemsSource for cbox2 based on what the user has selected in cbox1. I was thinking that this could be accomplished with something similar to:
我可能过度简化了这个例子,但简而言之,我有三个枚举,我想用于 cbox2 的 .ItemsSource 绑定。我想根据用户在 cbox1 中选择的内容将适当的枚举设置为 cbox2 的 .ItemsSource。我在想这可以通过类似于以下内容的方法来完成:
cbox2.SetBinding(ComboBox.ItemsSourceProperty, new Binding("AppropriateEnumGoesHere"));
Unfortunately, this doesn't seem to be working. I don't get an error or anything but I also don't see my enum values being displayed in cbox2. Also, as you can see in my XAML for cbox1 above, I'm using a converter to display the description attribute of each enum value. The enums that I want to use as the .ItemsSource for cbox2 also have description attributes that I want to display instead of the raw enum values and I'm not sure how that should work from code, either. Can anyone point me in the right direction? Thanks!
不幸的是,这似乎不起作用。我没有收到错误或任何消息,但我也没有看到我的枚举值显示在 cbox2 中。此外,正如您在上面 cbox1 的 XAML 中所见,我使用转换器来显示每个枚举值的描述属性。我想用作 cbox2 的 .ItemsSource 的枚举也有我想要显示的描述属性,而不是原始枚举值,我也不确定它应该如何从代码中工作。任何人都可以指出我正确的方向吗?谢谢!
采纳答案by Sheridan
The easiest way to do this is to create a property to bind to the second ComboBox.ItemsSourceproperty. Now I've done this with classes many times, but not enums, so it mightnot work, but it should. Normally, my classes would have to derive from one base class, so that the property could be of that type. In your case, you could try this:
最简单的方法是创建一个属性来绑定到第二个ComboBox.ItemsSource属性。现在我已经用类做了很多次了,但不是enums,所以它可能不起作用,但它应该。通常,我的类必须从一个基类派生,以便属性可以属于该类型。在你的情况下,你可以试试这个:
public ObservableCollection<Enum> Collection2 { get; set; } // Implement INPC interface
...
<ComboBox x:Name="cbox2" ItemsSource="{Binding Collection2}" ... />
Now lets assume that you have other collections that contain all of the values of your enums:
现在让我们假设您有其他包含您enums 的所有值的集合:
private List<BmwColours> bmwColours; // Fill these collections with correct values
private List<ToyotaColours> toyotaColours;
private List<FordColours> fordColours;
When the user makes a selection from the first ComboBox, you can fill the Collection2property with the correct enumvalues something like this:
当用户从第一个中进行选择时ComboBox,您可以Collection2使用正确的enum值填充属性,如下所示:
Collection2 = new ObservableCollection<Enum>();
if (cbox1.SelectedItem == ModeOfTransportationEnum.BmwX5)
foreach (Enum enumInstance in bmwColours) Collection2.Add(enumInstance);
else if (cbox1.SelectedItem == ModeOfTransportationEnum.ToyotaCamry)
foreach (Enum enumInstance in toyotaColours) Collection2.Add(enumInstance);
...
Now Collection2is full of the correct enumvalues. You might need to tweak this a bit to suit your needs, but it should do the trick.
现在Collection2充满了正确的enum价值观。您可能需要稍微调整一下以满足您的需求,但它应该可以解决问题。

