在ComboBox中选择的项目决定了ListBox中的项目

3
我有一个小的WPF应用程序,其中包含一个ComboBox,用户可以从列表中选择项目。我还有一个ListBox,希望在其中可用的项目取决于ComboBox当前选择的项目。
假设ComboBox有以下选项:“水果”和“蔬菜”。如果我选择“水果”,ListBox将包含“苹果”,“香蕉”,“梨子”等,如果我选择“蔬菜”,则将包含“胡萝卜”,“土豆”等。
这只是一个虚构的例子,但涵盖了我需要的内容。在我的应用程序中,无论是ComboBox的数据还是要放入ListBox中的任何内容都将来自外部数据源。
我应该如何做?我已经完成了视图模型到视图的绑定,并从数据源填充了ComboBox,但我需要ListBox的内容反映在ComboBox中选择的选项。

你获取 ListBox 的数据是以什么格式呈现的? - Herdo
ObservableCollection<string> - msk
那么你并不知道 ObservableCollection<string> 实际上属于哪个 ComboBox 成员的映射关系,对吗? - Herdo
3个回答

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

如果您正在使用视图模型,为什么要用代码来解决这个问题?我认为这应该在您的视图模型中处理。 - ar53nic

0
您可以在您的视图模型中添加一个 DependencyProperty,其中包含所选项目。
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替换为您的视图模型的类型)

并将其绑定到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.
    }

0

视图:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接