根据另一个下拉列表中的选择限制一个下拉列表中的项目

Limiting items in one drop-down based on selection in another drop-down

我有两个下拉框A和B,根据我select在下拉框A中的内容,我想限制在B中显示的项目。有没有属性我可以绑定 xaml 还是有其他方法可以实现?我正在使用 VS 2012、WPF、MVVM 模型和 telerik 控件。

想法是让两个列表都存在于视图模型中,加上两个属性来保存所选项目(数据项)。

将两个 ComboBox 绑定到这些属性,并在视图模型中处理 'FirstSelectedItem' 属性 更改(或进入其 setter)。

这样你就可以修改第二个列表的成员了。

让我们考虑国家和州组合框的示例,其中将根据国家组合框上的选择填充州组合框。

所以,如果我说的是 XAML 属性,这里您要在 Country 组合框的 SelectedItem 属性 的基础上更新 State 组合框的 ItemsSource 属性。

要实现此目的,请在 ViewModel 中添加一个新的 属性 "SelectedCountry",它将保存国家/地区组合框的选择。在这个 "SelectedCountry" 属性 的 Setter 中,根据需要设置 StateCollection。

确保实现 INotifyPropertyChanged 接口并为两个集合使用 ObservableCollection 类型。

以下是相同的代码示例:

Model Classes:

public class Country
{
    public string CountryName { get; set; }
    public int CountryId { get; set; }
    public List<State> States { get; set; }
}

public class State
{
    public string StateName { get; set; }
    public int StateId { get; set; }
}

ViewModel :

public class MainWindowViewModel : INotifyPropertyChanged
{

    public MainWindowViewModel()
    {
        CountriesCollection = new ObservableCollection<Country>();
        StateCollection = new ObservableCollection<State>();
        LoadData();
    }

    private ObservableCollection<Country> _CountriesCollection;

    public ObservableCollection<Country> CountriesCollection
    {
        get { return _CountriesCollection; }
        set
        {
            _CountriesCollection = value;
            NotifyPropertyChanged("CountriesCollection");
        }
    }

    private ObservableCollection<State> _StatesCollection;

    public ObservableCollection<State> StateCollection
    {
        get { return _StatesCollection; }
        set
        {
            _StatesCollection = value;
            NotifyPropertyChanged("StateCollection");
        }
    }

    private Country _SelectedCountry;

    public Country SelectedCountry
    {
        get { return _SelectedCountry; }
        set
        {
            _SelectedCountry = value;
            if (_SelectedCountry != null && _SelectedCountry.States != null)
            {
                StateCollection = new ObservableCollection<State>(_SelectedCountry.States);
            }
            NotifyPropertyChanged("SelectedCountry");
        }
    }

    private void LoadData()
    {
        if (CountriesCollection != null)
        {
            CountriesCollection.Add(new Country
            {
                CountryId = 1,
                CountryName = "India",
                States = new List<State>
                            {
                                    new State { StateId = 1, StateName = "Gujarat"},
                                    new State { StateId = 2, StateName = "Punjab"},
                                    new State { StateId = 3, StateName = "Maharastra"}
                            }
            });
            CountriesCollection.Add(new Country
            {
                CountryId = 2,
                CountryName = "Chine",
                States = new List<State>
                            {
                                    new State { StateId = 4, StateName = "Chine_State1"},
                                    new State { StateId = 5, StateName = "Chine_State2"},
                                    new State { StateId = 6, StateName = "Chine_State3"}
                            }
            });
            CountriesCollection.Add(new Country
            {
                CountryId = 3,
                CountryName = "japan",
                States = new List<State>
                            {
                                    new State { StateId = 7, StateName = "Japan_State1"},
                                    new State { StateId = 8, StateName = "Japan_State2"},
                                    new State { StateId = 9, StateName = "Japan_State3"}
                            }
            });
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info));
    }

}

XALM :

<StackPanel Orientation="Horizontal" >
        <ComboBox 
              Height="30" Width="100" 
              HorizontalAlignment="Left" Margin="30" 
              ItemsSource="{Binding CountriesCollection}" 
              SelectedItem="{Binding SelectedCountry}"
              DisplayMemberPath="CountryName">
        </ComboBox>
        <ComboBox
              Height="30" Width="100" 
              HorizontalAlignment="Left" Margin="30"  
              ItemsSource="{Binding SelectedCountry.States}"
              DisplayMemberPath="StateName">
        </ComboBox>
    </StackPanel>

XAML.CS

InitializeComponent();
this.DataContext = new MainWindowViewModel();

我希望这个例子能让你明白。如果您需要这方面的更多信息,请告诉我。