本文介绍了从datagridview中的组合框中删除已选择的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在模板列中有一个wpf datagridview和combobox。我不想显示已为其他行选择的值,但在执行此操作时,当前选中的项目也会被删除。 模型如下 b $ b Hi,I have a wpf datagridview and and combobox in the template column. I do not want to show the values already selected for other rows, but in doing this the item that is currently selected also gets removed.The model is as followspublic class MetadataConfigurationModel : ViewModelBase { private ObservableCollection<MetadataConfiguration> _metadataConfiguration = new ObservableCollection<MetadataConfiguration>(); public ObservableCollection<MetadataConfiguration> MetadataConfig { get { return _metadataConfiguration; } set { _metadataConfiguration = value; } } private ObservableCollection<TaxonomyField> _taxonomyList = new ObservableCollection<TaxonomyField>(); public ObservableCollection<TaxonomyField> TaxonomyList { get { ObservableCollection<TaxonomyField> tempTaxanomy = new ObservableCollection<TaxonomyField>(_taxonomyList); tempTaxanomy.Add(null); tempTaxanomy.Move(tempTaxanomy.Count - 1, 0); foreach (MetadataConfiguration mdConfig in _metadataConfiguration.ToList()) { tempTaxanomy.Remove(mdConfig.Field); } return tempTaxanomy; } set { _taxonomyList = value; } } public class MetadataConfiguration : ViewModelBase { private TaxonomyField _field; [XmlIgnore] public TaxonomyField Field { get { return _field; } set { _field = value; _fieldName = _field == null ? String.Empty : _field.Title; _fieldGUID = _field == null ? Guid.Empty : _field.Id; _termsetGUID = _field == null ? Guid.Empty : _field.TermSetId; OnPropertyChanged("Field"); } } private string _fieldName; public string FieldName { get { return _fieldName; } set { _fieldName = value; OnPropertyChanged("FieldName"); } } private Guid _fieldGUID; public Guid FieldGUID { get { return _fieldGUID; } set { _fieldGUID = value; OnPropertyChanged("FieldGUID"); } } private Guid _termsetGUID; public Guid TermsetGUID { get { return _termsetGUID; } set { _termsetGUID = value; OnPropertyChanged("TermsetGUID"); } } } } ViewModel按如下方式填充列表 The ViewModel populates the list as follows_model.TaxonomyList = new ObservableCollection<TaxonomyField>(_lstTaxonomyFileds); if (_model.MetadataConfig.Count > 0) { foreach (MetadataConfigurationModel.MetadataConfiguration mdConfig in _model.MetadataConfig) { mdConfig.Field = (from a in _model.TaxonomyList where a.Id == mdConfig.FieldGUID select a).FirstOrDefault(); } } 和xaml如下 and the xaml is as follows<DataGrid AutoGenerateColumns="False" x:Name="dgMetadataConfig" Grid.Row="2" Grid.Column="0" ScrollViewer.VerticalScrollBarVisibility="Auto" ItemsSource="{Binding MetadataConfig, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="10,0,0,0" CanUserAddRows="False" VerticalAlignment="Top"> <DataGrid.Columns> <DataGridTemplateColumn Header="Field Name"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox Width="170" SelectedItem="{Binding Field, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"ItemsSource="{Binding DataContext.TaxonomyList, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, UpdateSourceTrigger=PropertyChanged}" Margin="2,2,2,2" DropDownOpened="CmbTaxonomyField_DropDownOpened" DisplayMemberPath="Title" SourceUpdated="CmbTaxonomyField_SourceUpdated"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns></DataGrid> 任何帮助将不胜感激。 问候, Zafar Any help will be appreciated.Regards,Zafar推荐答案 这篇关于从datagridview中的组合框中删除已选择的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-10 02:28