使用combobox1确定要在第二个组合框2上显示的值

时间:2014-04-12 23:03:01

标签: c# wpf

我有2个组合框。我想设置它,以便当用户选择第一个组合框上的信息时,这将决定在第二个组合框上显示的内容。

For example combobox1 has 2 listboxitems:
Fruit
Cars

If Fruit is selected on first combobox, the second combobox will display listboxes:

Apple
Orange

If Cars is selected on first combobox, the second combobox will display listboxes:

Honda
Nissan

有办法做到这一点吗?

<ComboBox Name="comboBox1" Canvas.Left="74" Canvas.Top="527" Width="120">
                <ListBoxItem Content="Fruit"/>
                <ListBoxItem Content="Car"/>
            </ComboBox>

<ComboBox Name="comboBox2" Canvas.Left="74" Canvas.Top="527" Width="120">
                <ListBoxItem Content="Apple"/>
                <ListBoxItem Content="Orange"/>

                <ListBoxItem Content="Honda"/>
                <ListBoxItem Content="Nissan"/>
            </ComboBox>

2 个答案:

答案 0 :(得分:1)

好的,这是使用DataBinding和关联的ViewModel的简单示例。

ViewModel用于保存ItemTypes和Items列表。使用DataBinding,ComboBox ItemsSource绑定到适用的公共属性。每当选择新的ItemType时,都会过滤Items列表。

<强>的Xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:converters="clr-namespace:WpfApplication1"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <ComboBox Name="comboBox1" Width="120" Margin="0,25,0,50" ItemsSource="{Binding Types}" SelectedItem="{Binding SelectedType}"/>
        <ComboBox Name="comboBox2" Width="120" ItemsSource="{Binding Items}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Value}"/>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </StackPanel>
</Window>

<强> Xaml.cs

这用于将窗口的DataContext设置为ViewModel。还有其他方法,但这是最简单的示范。

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    MainViewModel vm = new MainViewModel();
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = vm;
    }
}

<强>视图模型

这是所有逻辑在数据,过滤等方面存在的地方.NonifyPropertyChanged是一个事件,只要绑定属性发生更改就需要引发它,以便它可以通知控件更新。

namespace WpfApplication1
{
    public class MainViewModel : INotifyPropertyChanged
    {
        #region Members
        private ItemType _selectedType;
        private List<Item> _allItems;
        private List<Item> _items;
        #endregion Members

        #region Properties
        /// <summary>
        /// Gets or sets the Selected Item Type
        /// </summary>
        /// <value>
        /// The selected Item Type
        /// </value>
        public ItemType SelectedType
        {
            get { return _selectedType; }
            set
            {
                _selectedType = value;
                Filter(); // Filter items list once SelectedType has changed
            }
        }

        /// <summary>
        /// Gets a list of all Item Types
        /// </summary>
        public List<ItemType> Types { get; private set; }

        /// <summary>
        /// Gets or sets the currently filltered list of Items
        /// </summary>
        /// <value>
        /// The fitlered items.
        /// </value>
        public List<Item> Items 
        {
            get { return _items; }
            set
            {
                _items = value;
                NotifyPropertyChanged("Items");
            }
        }
        #endregion Properties

        public MainViewModel()
        {
            Types = new List<ItemType>
            {
                ItemType.All,
                ItemType.Fruit,
                ItemType.Car
            };

            _allItems = new List<Item>
            {
                new Item
                {
                    Value = "Apple",
                    Type = ItemType.Fruit
                },
                new Item
                {
                    Value = "Orange",
                    Type = ItemType.Fruit
                },
                new Item
                {
                    Value = "Honda",
                    Type = ItemType.Car
                },
                new Item
                {
                    Value = "Nissan",
                    Type = ItemType.Car
                }
            };
            Items = _allItems;
        }

        /// <summary>
        /// Filters the Items list based on currently selected Item Type
        /// </summary>
        private void Filter()
        {
             var filteredItems = new List<Item>();
             if (ItemType.All == _selectedType)
             {
                 filteredItems = _allItems;
             }
             else 
             {
                 foreach (var item in _allItems)
                 {
                     if (item.Type == _selectedType)
                     {
                         filteredItems.Add(item);
                     }
                 }
             }
             Items = filteredItems;
        }

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }

    public enum ItemType
    {
        All,
        Fruit,
        Car
    }

    public class Item
    {
        public string Value { get; set; }
        public ItemType Type { get; set; }
    }
}

答案 1 :(得分:0)

您可以为每一组(汽车和水果)创建一个集合,然后清除组合框并根据您选择的对象将集合添加到其中