在另一个中设置 3 个不同的“observableCollection”?

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


我想做的事:


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


我完成了结构,它适用于列表,现在我必须成功使用对象;


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


    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-


我希望我说清楚了,我不知道最好的解决方案是什么,我真的很想在这个周末之前完成这个,哈哈


编辑:(“服务”数据类型是“ObservableCollection-Services-”,公司是“ObservableCollection-Company-”)


预先感谢您的建议!


隔江千里
浏览 76回答 1
1回答

慕哥9229398

您可以使用cmb1 的选择DataTrigger权来设置ItemsSource,如果Cmb1SelectedItem更改其更改 Cmb2 Itemsource 无需在 VM 中维护&nbsp; &nbsp; &nbsp;<ComboBox Name="Cmb1" ItemsSource="{Binding Cmb1List}" SelectedItem="{Binding Cmb1SelectedItem}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ComboBox>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ComboBox Name="Cmb2"&nbsp; >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ComboBox.Style>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Style TargetType="{x:Type ComboBox}">&nbsp;<Setter Property="ItemsSource" Value="{Binding ObservableCollectionC}"></Setter>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Style.Triggers>&nbsp; &nbsp; <DataTrigger Binding="{Binding Cmb1SelectedItem}" Value="A">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="ItemsSource" Value="{Binding ObservableCollectionA}"></Setter>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </DataTrigger>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataTrigger Binding="{Binding Cmb1SelectedItem}" Value="B">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="ItemsSource" Value="{Binding ObservableCollectionB}"></Setter>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </DataTrigger>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Style.Triggers>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Style>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ComboBox.Style>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ComboBox>
打开App,查看更多内容
随时随地看视频慕课网APP