更改组合框中内容的完整列表

更改组合框中内容的完整列表

本文介绍了更改组合框中内容的完整列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,我有一个comboBoxlistBox. listBox充满了不同的命令. selectedItemcomboBox的内容取决于选择哪个命令. comboBox中的selectedItem保持为该命令选择的状态.例如,当用户在comboBox中选择了内容,然后切换到另一个命令,然后又切换回去,则仍将在comboBox中选择相同的项目.

In my program I have a comboBox and a listBox. The listBox is full of different commands. The selectedItem and contents of the comboBox depend on which command is selected. The selectedItem in the comboBox stays selected for that command. For example, when the user has content selected in the comboBox and they switch to a different command, and then switch back, the same item will still be selected in the comboBox.

因为comboBox填充了不同的项目,具体取决于选择了哪个命令,所以我应该为每组项目制作一个ObservableCollection吗?我只允许将ItemsSource绑定到一件事,那怎么办?

Because the comboBox gets populated with different items depending on which command is selected should I make an ObservableCollection for each set of items? I'm only allowed to bind the ItemsSource to one thing, so how would that work?

如果这不是正确的方法,请提出建议.如果还不够清楚,请告诉我.

If that's not the right approach please advise. If this isn't clear enough, please let me know.

谢谢!

以下是我的程序的示例:

Here's a sample of what my program will look like:

然后,如果选择了命令2,则例如,组合框中的列表可能是a,b,c,d,e.

Then, if command 2 is selected, the list in the combo box might be a, b, c, d, e, for example.

推荐答案

在我看来,您正在这里寻找Master/Detail结构:

Seems to me you're looking for a Master / Detail structure here:

数据项:

public class MyCommandDefinition
{
    public string DisplayName {get;set;}

    public List<MyParameter> Parameters {get;set;}

    public Parameter SelectedParameter {get;set;}
}

public class MyParameter
{
    public string DisplayName {get;set;}

    //Additional properties depending on your needs.
}

ViewModel:

ViewModel:

public class MyViewModel
{
    public List<MyCommandDefinition> Commands {get;set;}

    public MyCommandDefinition SelectedCommand {get;set;}
}

XAML:

<ListBox ItemsSource="{Binding Commands"}"
         SelectedItem="{Binding SelectedCommand}"
         DisplayMemberPath="DisplayName"/>

<ComboBox ItemsSource="{Binding SelectedCommand.Parameters}"
          SelectedItem="{Binding SelectedCommand.SelectedParameter}"
          DisplayMemberPath="DisplayName"/>

如果要以编程方式更改它们,并希望它们会反映在UI中,请不要忘记所有这些属性中的NotifyPropertyChanged().

Don't forget NotifyPropertyChanged() in all these properties if you're going to change them programatically and expect that to be reflected in the UI.

这篇关于更改组合框中内容的完整列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:28