繁体   English   中英

使用combobox1确定要在第二个combobox2上显示的值

[英]Using a combobox1 to determine the values to be displayed on the second combobox2

我有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>

好的,这是一个使用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;
    }
}

ViewModel

这是所有逻辑都依赖于数据,筛选等的地方。NotifyPropertyChanged是一个事件,只要绑定属性发生更改,就需要引发该事件,以便它可以通知控件更新。

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; }
    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM