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

时间:2017-04-26 02:30:53

标签: wpf xaml mvvm telerik

我有两个下拉列表A和B.基于我在下拉列表A中选择的内容,我想限制B中显示的项目。是否有可以在xaml中绑定的属性或者是否有其他方法可以实现这一目标?我正在使用VS 2012,WPF,MVVM模型和telerik控件。

2 个答案:

答案 0 :(得分:2)

让我们考虑Country和State组合框的示例,其中将根据Country组合框中的选择填充状态组合框。

所以,如果我说的是XAML属性,那么你想根据Country组合框的SelectedItem属性更新State组合框的ItemsSource属性。

要实现这一目标,请添加一个新属性" SelectedCountry"在ViewModel中,它将保存Country组合框的选择。在Setter of this" SelectedCountry"属性,根据需要设置StateCollection。

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

以下是相同的代码示例:

  

模型类:

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();

我希望这个例子能让你清楚明白。如果您需要更多相关信息,请与我们联系。

答案 1 :(得分:0)

我们的想法是在viewmodel中存在两个列表,以及两个用于保存所选项(数据项)的属性。

将两个ComboBox绑定到这些属性,并在viewmodel句柄中更改'FirstSelectedItem'属性(或输入其setter)。

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