【问题标题】:Let items in ListBox depend on which item is selected in a ComboBox让 ListBox 中的项目取决于在 ComboBox 中选择了哪个项目
【发布时间】:2015-10-20 07:44:14
【问题描述】:

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

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

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

我该怎么做?我已经完成了视图模型与视图的绑定,并从数据源填充了 ComboBox,但我需要 ListBox 的内容来反映 ComboBox 中的选定选项。

【问题讨论】:

  • 你以什么格式获取ListBox的数据?
  • ObservableCollection
  • 所以您对映射一无所知,ObservableCollection<string> 属于 ComboBox 值的哪个成员?

标签: c# .net wpf xaml mvvm


【解决方案1】:

制作 2 个列表并根据选择将其中一个绑定到您的列表框。例如:

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

在您的 Combox 选择更改事件中:

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

【讨论】:

  • 如果您使用的是视图模型,为什么要用后面的代码来解决这个问题?我认为这应该在您的视图模型中处理。
【解决方案2】:

您可以将 DependencyProperty 添加到包含 SelectedItem 的视图模型中

public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register(
        "SelectedItem",
        typeof(YourType),
        typeof(YourViewModel),
        new FrameworkPropertyMetadata(
            new PropertyChangedCallback(OnSelectedItemChanged)
        )
    );

    public YourType SelectedItem
    {
        get { return (YourType)GetValue(SelectedItemProperty); }
        set { SetValue(SelectedItemProperty, value); }
    }

(将 YourType 替换为蔬菜/水果的类型,将 YourViewModel 替换为您的 viemodel 的类型)

并将其绑定到 XAML 中的 Combobox SelectedItem。

<ComboBox x:Name="comboBox" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}">

你还需要定义一个方法来处理PropertyChangedCallback:

    private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        // Change the listbox item source here.
    }

【讨论】:

    【解决方案3】:

    查看:

    <ComboBox ItemsSource="{Binding Options}" SelectedItem="{Binding SelectedOption}"
        Width="200"/>
    <ListBox ItemsSource="{Binding lst}" Grid.Row="1">
    

    视图模型:

    public class MainViewModel:INotifyPropertyChanged
    {
        private string selectedOption;
    
        public string SelectedOption
        {
            get
            {
                return this.selectedOption;
            }
            set
            {
                this.selectedOption = value;
                this.UpdateOnOptionChange();
            }
        }
        
        public List<string> Options
        {
            get;
            set;
        }
    
        public ObservableCollection<string> lst
        {
            get;
            set;
        }
    
        public MainViewModel()
        {
            this.Options = new List<string>() { "Fruits", "Vegetables" };
            this.lst = new ObservableCollection<string>();
        }
    
        private void UpdateOnOptionChange()
        {
            this.lst.Clear();
            if (this.selectedOption == "Fruits")
            {
                this.lst.Add("Apple");
                this.lst.Add("Banana");
                this.lst.Add("Pear");
            }
            else if (this.selectedOption == "Vegetables")
            {
                this.lst.Add("Carrot");
                this.lst.Add("Potato");
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public void NotifyOnPropertyChange(string astrPropertyName)
        {
            if (null != this.PropertyChanged)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(astrPropertyName));
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多