在 MvvM 模型中显示由 属性 更改事件驱动的组合框列表

Display a combobox list driven from property changed event in a MvvM model

我是 MVVM 的新手,我正在我的模型中编写一个包含 2 个组合框的搜索视图。我需要根据第一个组合框选择在第二个组合框中显示各种列表。组合框 1 如下所示

<ComboBox Margin="103,6,0,24" Name="cmbCriteria" DisplayMemberPath="SetCriteria" Grid.Column="1">
                        <ComboBox.ItemsSource>
                            <CompositeCollection>
                                <CollectionContainer Collection="{Binding Source={x:Static model:SearchCriteriaStaicData.MyStaicCriteriaList}}" />
                            </CompositeCollection>
                        </ComboBox.ItemsSource>
                    </ComboBox>

这显示了我后面的代码中的数据

{
        public string SetCriteria { get; set; }

    }    
    public class SearchCriteriaStaicData
    {
        public static List<SearchCriteriaList> MyStaicCriteriaList
        {
            get
            {
                return new List<SearchCriteriaList>()
                {
                    new SearchCriteriaList {SetCriteria = "Date Range"},
                    new SearchCriteriaList {SetCriteria = "Gateway"},
                };
            }
        }
    }

我想做的是如果选择的组合框值是 Date Range 那么 我的第二个组合框在我的 SearchDateOperatorStaticData class

中显示列表
public class SearchDateOperatorStaticData
   {
       public static List<SearchOperatorLists> DateStaicOperatorList
       {
           get 
           {
               return new List<SearchOperatorLists>()
               {
                   new SearchOperatorLists {OperatorName = "Today"},
                   new SearchOperatorLists {OperatorName = "Between"},
                   new SearchOperatorLists {OperatorName = "Last Month"},
               };
           }
       }     
   }

或者,如果从第一个组合框中选择网关,则第二个组合框会显示来自 SearchGatewayOperatorStaticData 的列表

public class SearchGatewayOperatorStaticData
   {
       public static List<SearchOperatorLists> GatewayStaicOperatorList
       {
           get
           {
               return new List<SearchOperatorLists>()
               {
                   new SearchOperatorLists {OperatorName = "Email"},
                   new SearchOperatorLists {OperatorName = "RDP"},
                   new SearchOperatorLists {OperatorName = "Web"},
                   new SearchOperatorLists {OperatorName = "Other"},
               };
           }
       }  
   }

因为你添加了mvvm标签,我建议你遵循MVVM模式,将逻辑移到视图模型中。

创建将包含您的集合的视图模型:

class ViewModel : INotifyPropertyChanged
{
    private readonly List<SearchOperatorLists> dateOperatorList = new List<SearchOperatorLists> 
    {
        new SearchOperatorLists {OperatorName = "Today"},
        new SearchOperatorLists {OperatorName = "Between"},
        new SearchOperatorLists {OperatorName = "Last Month"} 
    };

    private readonly List<SearchOperatorLists> gatewayOperatorList = new List<SearchOperatorLists> 
    {
        new SearchOperatorLists {OperatorName = "Email"},
        new SearchOperatorLists {OperatorName = "RDP"},
        new SearchOperatorLists {OperatorName = "Web"},
        new SearchOperatorLists {OperatorName = "Other"} 
    };

    private SearchCriteriaList selectedCriteriaList;

    public ViewModel()
    {
        this.CriteriaList = new ObservableCollection<SearchCriteriaList> 
        {
            new SearchCriteriaList {SetCriteria = "Date Range"},
            new SearchCriteriaList {SetCriteria = "Gateway"} 
        };

        this.OperatorList = new ObservableCollection<SearchOperatorLists>();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public ObservableCollection<SearchCriteriaList> CriteriaList { get; private set; }

    public SearchCriteriaList SelectedCriteriaList 
    {
        get { return this.selectedCriteriaList; }
        set
        {
            if (value != this.selectedCriteriaList)
            {
                this.selectedCriteriaList = value;
                this.OnPropertyChanged("SelectedCriteriaList");
                this.UpdateOperators();
            }
        }
    }

    public ObservableCollection<SearchOperatorLists> OperatorList { get; private set; }

    private void UpdateOperators()
    {
        this.OperatorList.Clear();

        if (this.SelectedCriteriaList == this.CriteriaList[0])
        {
            foreach (SearchOperatorLists item in this.dateOperatorList)
            {
                this.OperatorList.Add(item);
            }
        }
        else if (this.SelectedCriteriaList == this.CriteriaList[1])
        {
            foreach (SearchOperatorLists item in this.gatewayOperatorList)
            {
                this.OperatorList.Add(item);
            }
        }
    }

    private void OnPropertyChanged(string property)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

请注意,视图模型包含一个 属性 SelectedCriteriaList,它绑定到组合框的当前选择项。当此值更改时(例如,通过组合框中的用户选择),您的视图模型将完成其工作并调用 UpdateOperators() 方法。此方法将更新 OperatorList 集合,使其项目符合所选条件。

然后您的视图应使用绑定:

<ComboBox 
    DisplayMemberPath="SetCriteria" 
    ItemsSource="{Binding CriteriaList}" 
    SelectedItem="{Binding SelectedCriteriaList, Mode=TwoWay}"/>

<ComboBox 
    DisplayMemberPath="OperatorName" 
    ItemsSource="{Binding OperatorList}"/>