让列表框中的项依赖于在组合框中选择的项

本文关键字:组合 选择 依赖于 列表 | 更新日期: 2023-09-27 18:05:14

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

假设组合框有以下选项:"水果"answers";Vegetables"。如果我选择"水果",列表框将包含"苹果"、"香蕉"、"梨";等等,如果我选择"蔬菜"它将包含"胡萝卜","土豆";等。

这只是一个虚构的例子,但涵盖了我需要的。在我的应用程序-无论是数据的组合框和什么是要放在ListBox将来自外部数据源。

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

让列表框中的项依赖于在组合框中选择的项

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

你可以添加一个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.
    }

视图:

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

ViewModel:

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