赞
踩
1.View代码
- <DataGrid ItemsSource="{Binding DataItems}" AutoGenerateColumns="False">
- <DataGrid.Columns>
- <DataGridTemplateColumn Header="First ComboBox">
- <DataGridTemplateColumn.CellTemplate>
- <DataTemplate>
- <ComboBox ItemsSource="{Binding FirstComboBoxItems}" SelectedItem="{Binding FirstComboBoxItem, Mode=TwoWay}" />
- </DataTemplate>
- </DataGridTemplateColumn.CellTemplate>
- </DataGridTemplateColumn>
- <DataGridTemplateColumn Header="Second ComboBox">
- <DataGridTemplateColumn.CellTemplate>
- <DataTemplate>
- <ComboBox ItemsSource="{Binding SecondComboBoxItems}" SelectedItem="{Binding SecondComboBoxItem, Mode=TwoWay}" />
- </DataTemplate>
- </DataGridTemplateColumn.CellTemplate>
- </DataGridTemplateColumn>
- </DataGrid.Columns>
- </DataGrid>
ViewModel代码
- public class MainViewModel : INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- private ObservableCollection<DataItem> _dataItems;
- public ObservableCollection<DataItem> DataItems
- {
- get { return _dataItems; }
- set
- {
- _dataItems = value;
- OnPropertyChanged("DataItems");
- }
- }
- private ObservableCollection<string> _firstComboBoxItems;
- public ObservableCollection<string> FirstComboBoxItems
- {
- get { return _firstComboBoxItems; }
- set
- {
- _firstComboBoxItems = value;
- OnPropertyChanged("FirstComboBoxItems");
- }
- }
- private ObservableCollection<string> _secondComboBoxItems;
- public ObservableCollection<string> SecondComboBoxItems
- {
- get { return _secondComboBoxItems; }
- set
- {
- _secondComboBoxItems = value;
- OnPropertyChanged("SecondComboBoxItems");
- }
- }
- private string _selectedFirstComboBoxItem;
- public string SelectedFirstComboBoxItem
- {
- get { return _selectedFirstComboBoxItem; }
- set
- {
- _selectedFirstComboBoxItem = value;
- OnPropertyChanged("SelectedFirstComboBoxItem");
- //第一个下拉框选择后更新第二个下拉框的下拉选项
- UpdateSecondComboBoxItems();
- }
- }
- private string _selectedSecondComboBoxItem;
- public string SelectedSecondComboBoxItem
- {
- get { return _selectedSecondComboBoxItem; }
- set
- {
- _selectedSecondComboBoxItem = value;
- OnPropertyChanged("SelectedSecondComboBoxItem");
- }
- }
- public MainViewModel()
- {
- DataItems = new ObservableCollection<DataItem>();
- FirstComboBoxItems = new ObservableCollection<string>();
- SecondComboBoxItems = new ObservableCollection<string>();
- //填充DataItems
- DataItems.Add(new DataItem { FirstComboBoxItem = "Item1", SecondComboBoxItem = "SubItem1" });
- DataItems.Add(new DataItem { FirstComboBoxItem = "Item1", SecondComboBoxItem = "SubItem2" });
- DataItems.Add(new DataItem { FirstComboBoxItem = "Item2", SecondComboBoxItem = "SubItem3" });
- DataItems.Add(new DataItem { FirstComboBoxItem = "Item2", SecondComboBoxItem = "SubItem4" });
- //填充FirstComboBoxItems
- FirstComboBoxItems.Add("Item1");
- FirstComboBoxItems.Add("Item2");
- }
- private void UpdateSecondComboBoxItems()
- {
- SecondComboBoxItems.Clear();
- foreach (DataItem item in DataItems)
- {
- if (item.FirstComboBoxItem == SelectedFirstComboBoxItem)
- {
- SecondComboBoxItems.Add(item.SecondComboBoxItem);
- }
- }
- }
- protected void OnPropertyChanged(string name)
- {
- PropertyChangedEventHandler handler = PropertyChanged;
- if (handler != null)
- {
- handler(this, new PropertyChangedEventArgs(name));
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。