设置3个不同的“ observableCollection”在另一个? [英] Set 3 different "observableCollection" in another one?

查看:66
本文介绍了设置3个不同的“ observableCollection”在另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVVM中工作,我发现了这种模式。

I work in MVVM and I discover this pattern.

我要做什么:

2个组合框:
如果组合框1显示A,我想要组合框2显示 observableCollectionA列表
如果组合框1显示B,我希望组合框2显示 observableCollectionB
如果组合框1显示C,我希望组合框2显示 observableCollectionC

2 comboboxes: If combobox 1 displays A, I want combobox 2 to display the list of "observableCollectionA" If combobox 1 displays B, I want combobox 2 to display "observableCollectionB" If combobox 1 displays C, I want combobox 2 to display "observableCollectionC"

我完成了结构,它可以与list一起使用,现在我必须使用对象成功;

I finished the structure, it works with list, now I have to succeed with objects;

这里是组合框中采用选定值 SelectedValue的代码,并将其发送到要显示的结果。在这里,我想比较组合框中的值(例如:公司),并进行比较以获取公司列表并在第二个组合框中显示它:

Here is the code that takes the selected value "SelectedValue" in the combobox, and sends it to the result to be displayed. This is where I want to compare the value in my combobox (example: "Company") and compare it to get my company list and display it in the second combobox :

    private string _SelectedListValue;
    public string SelectedListValue
    {
        get
        {
            return _SelectedListValue;
        }
        set
        {
            if (value != _SelectedListValue)
            {
                _SelectedListValue = value;
                RaisePropertyChanged(nameof(SelectedListValue));
                ResultList = new ObservableCollection<string>();

                if (value == "Company")
                {
                    _ResultList.Add("Hello"); //Test, it works
                    _ResultList = Company; 
                }
                else if(value == "Services")
                {
                    _ResultList.Add("Not Hello");//test, it Works
                    _ResultList = Services;
                }
            }
        }
    }

第二个组合框:

    private ObservableCollection<string> _ResultList;
    public ObservableCollection<string> ResultList
    {
        get
        {
            return _ResultList;
        }
        set
        {
            if (value != _ResultList)
            {
                _ResultList = value;
                RaisePropertyChanged(nameof(ResultList));
            }
        }
    }

以下是我的数据:

        Company = new ObservableCollection<Company>((await _dataService.GetCompany().ConfigureAwait(false)));
        Services = await _dataService.GetServicesAsync(true).ConfigureAwait(false);
        Sections = await _dataService.GetSectionsAsync(_dataService.ParamGlobaux.IDCompany).ConfigureAwait(false);

根据我的情况,如果 SelectedListValue的值为 Company,我希望这样做,然后 _ResultList加载ObservableCollection-Company-

What I would like is that according to my condition, if the value of "SelectedListValue" is "Company", then "_ResultList" loads the ObservableCollection-Company-

我希望我很清楚,我不知道最好的解决方案是什么,我真的很想

I hope I was clear, I don't know what the best solution is and I'd really like to finish this before this weekend ahah

编辑:( Services数据类型为 ObservableCollection-Services-,而Company为 ObservableCollection-Company-)

EDIT : ( "Services" data type is "ObservableCollection-Services-" and company is an "ObservableCollection-Company-")

预先感谢您的建议!

推荐答案

您可以使用 DataTrigger 如果选择 Cmb1SelectedItem ItemsSource >更改了Cmb2项目源,无需在VM中维护

you can use DataTrigger to set the right ItemsSource on selecection of cmb1, if the Cmb1SelectedItem changed its change the Cmb2 Itemsource no need to maintain in VM

     <ComboBox Name="Cmb1" ItemsSource="{Binding Cmb1List}" SelectedItem="{Binding Cmb1SelectedItem}">

            </ComboBox>

            <ComboBox Name="Cmb2"  >
                <ComboBox.Style>
                    <Style TargetType="{x:Type ComboBox}">
 <Setter Property="ItemsSource" Value="{Binding ObservableCollectionC}"></Setter>
                        <Style.Triggers>
    <DataTrigger Binding="{Binding Cmb1SelectedItem}" Value="A">
                                <Setter Property="ItemsSource" Value="{Binding ObservableCollectionA}"></Setter>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Cmb1SelectedItem}" Value="B">
                                <Setter Property="ItemsSource" Value="{Binding ObservableCollectionB}"></Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ComboBox.Style>
            </ComboBox>

这篇关于设置3个不同的“ observableCollection”在另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆