【问题标题】:Limiting items in one drop-down based on selection in another drop-down根据另一个下拉列表中的选择限制一个下拉列表中的项目
【发布时间】:2017-09-23 06:07:03
【问题描述】:

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

【问题讨论】:

  • 限制项目是什么意思?你试过任何代码吗?这个问题太笼统,无法回答。
  • 您似乎希望我们为您编写一些代码。虽然许多用户愿意为陷入困境的编码人员编写代码,但他们通常只有在发布者已经尝试自己解决问题时才会提供帮助。展示这项工作的一个好方法是包含您迄今为止编写的代码、示例输入(如果有的话)、预期输出和您实际获得的输出(控制台输出、回溯等)。您提供的详细信息越多,您可能收到的答案就越多。检查FAQHow to Ask
  • @PierreLebon 再次阅读我的帖子确实使它看起来像那样,我应该提供一些代码,尽管我正在寻找在 xaml 和 viewmodel 中处理场景的指导。负面标记对我来说似乎不是很合理,但我已经浏览了您提供的链接并感谢您的反馈。
  • @Simsons 抱歉,我的问题似乎有点含糊。通过限制项目,我的意思是第二个下拉列表中填充的项目将取决于我在第一个下拉列表中选择的项目。但我已经做到了,所以无论如何谢谢:)

标签: wpf xaml mvvm telerik


【解决方案1】:

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

将两个 ComboBox 绑定到这些属性,并在视图模型中处理“FirstSelectedItem”属性的更改(或对其设置器的条目)。

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

【讨论】:

  • 是的,谢谢您的输入,我已经实现了类似的方法:) 在第二个下拉列表的设置器中,我调用了一个函数,该函数将第一个下拉列表的选定项目作为参数提供给过程其余逻辑在哪里实现。这对我有用,再次感谢:)
【解决方案2】:

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

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

为此,在 ViewModel 中添加一个新属性“SelectedCountry”,它将保存国家组合框的选择。在此“SelectedCountry”属性的 Setter 中,根据需要设置 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; }
}

视图模型:

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

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

【讨论】:

  • 感谢您的详细解释和示例代码。尽管我已经设法实现它并且只是现在才通过您的答案,但我已经以与您的解决方案几乎相似的方式实现它,只有一些更改(就像我在设置器中使用 RaisePropertyChanged 而不是 NotifyPropertyChanged 并且因为我应该从数据库中获取下拉列表的数据,我决定在过程中实现它的逻辑)。你用你的代码为我澄清了一些事情,再次感谢:)
  • @VandanaChandola 很高兴我的回答对您有所帮助!如果我的帖子回答了您的问题,请点赞并将其标记为答案,以便将来对其他人也有用。
  • 已经尝试支持您的答案,但由于我的声誉低于 15,因此不会显示。
  • 是的,灰色的勾号。完成:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多