当前位置:   article > 正文

WPF MVVM模式DataGrid Combobox级联_wpf的mvvm的combox怎么使用

wpf的mvvm的combox怎么使用

1.View代码

  1. <DataGrid ItemsSource="{Binding DataItems}" AutoGenerateColumns="False">
  2. <DataGrid.Columns>
  3. <DataGridTemplateColumn Header="First ComboBox">
  4. <DataGridTemplateColumn.CellTemplate>
  5. <DataTemplate>
  6. <ComboBox ItemsSource="{Binding FirstComboBoxItems}" SelectedItem="{Binding FirstComboBoxItem, Mode=TwoWay}" />
  7. </DataTemplate>
  8. </DataGridTemplateColumn.CellTemplate>
  9. </DataGridTemplateColumn>
  10. <DataGridTemplateColumn Header="Second ComboBox">
  11. <DataGridTemplateColumn.CellTemplate>
  12. <DataTemplate>
  13. <ComboBox ItemsSource="{Binding SecondComboBoxItems}" SelectedItem="{Binding SecondComboBoxItem, Mode=TwoWay}" />
  14. </DataTemplate>
  15. </DataGridTemplateColumn.CellTemplate>
  16. </DataGridTemplateColumn>
  17. </DataGrid.Columns>
  18. </DataGrid>
  1. ViewModel代码

  1. public class MainViewModel : INotifyPropertyChanged
  2. {
  3. public event PropertyChangedEventHandler PropertyChanged;
  4. private ObservableCollection<DataItem> _dataItems;
  5. public ObservableCollection<DataItem> DataItems
  6. {
  7. get { return _dataItems; }
  8. set
  9. {
  10. _dataItems = value;
  11. OnPropertyChanged("DataItems");
  12. }
  13. }
  14. private ObservableCollection<string> _firstComboBoxItems;
  15. public ObservableCollection<string> FirstComboBoxItems
  16. {
  17. get { return _firstComboBoxItems; }
  18. set
  19. {
  20. _firstComboBoxItems = value;
  21. OnPropertyChanged("FirstComboBoxItems");
  22. }
  23. }
  24. private ObservableCollection<string> _secondComboBoxItems;
  25. public ObservableCollection<string> SecondComboBoxItems
  26. {
  27. get { return _secondComboBoxItems; }
  28. set
  29. {
  30. _secondComboBoxItems = value;
  31. OnPropertyChanged("SecondComboBoxItems");
  32. }
  33. }
  34. private string _selectedFirstComboBoxItem;
  35. public string SelectedFirstComboBoxItem
  36. {
  37. get { return _selectedFirstComboBoxItem; }
  38. set
  39. {
  40. _selectedFirstComboBoxItem = value;
  41. OnPropertyChanged("SelectedFirstComboBoxItem");
  42.             //第一个下拉框选择后更新第二个下拉框的下拉选项
  43. UpdateSecondComboBoxItems();
  44. }
  45. }
  46. private string _selectedSecondComboBoxItem;
  47. public string SelectedSecondComboBoxItem
  48. {
  49. get { return _selectedSecondComboBoxItem; }
  50. set
  51. {
  52. _selectedSecondComboBoxItem = value;
  53. OnPropertyChanged("SelectedSecondComboBoxItem");
  54. }
  55. }
  56. public MainViewModel()
  57. {
  58. DataItems = new ObservableCollection<DataItem>();
  59. FirstComboBoxItems = new ObservableCollection<string>();
  60. SecondComboBoxItems = new ObservableCollection<string>();
  61. //填充DataItems
  62. DataItems.Add(new DataItem { FirstComboBoxItem = "Item1", SecondComboBoxItem = "SubItem1" });
  63. DataItems.Add(new DataItem { FirstComboBoxItem = "Item1", SecondComboBoxItem = "SubItem2" });
  64. DataItems.Add(new DataItem { FirstComboBoxItem = "Item2", SecondComboBoxItem = "SubItem3" });
  65. DataItems.Add(new DataItem { FirstComboBoxItem = "Item2", SecondComboBoxItem = "SubItem4" });
  66. //填充FirstComboBoxItems
  67. FirstComboBoxItems.Add("Item1");
  68. FirstComboBoxItems.Add("Item2");
  69. }
  70. private void UpdateSecondComboBoxItems()
  71. {
  72. SecondComboBoxItems.Clear();
  73. foreach (DataItem item in DataItems)
  74. {
  75. if (item.FirstComboBoxItem == SelectedFirstComboBoxItem)
  76. {
  77. SecondComboBoxItems.Add(item.SecondComboBoxItem);
  78. }
  79. }
  80. }
  81. protected void OnPropertyChanged(string name)
  82. {
  83. PropertyChangedEventHandler handler = PropertyChanged;
  84. if (handler != null)
  85. {
  86. handler(this, new PropertyChangedEventArgs(name));
  87. }
  88. }
  89. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/928550
推荐阅读
相关标签
  

闽ICP备14008679号