Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 follows

C#
public 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");
                }
            }
        }
    }

The ViewModel populates the list as follows
C#
_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();
                }
            }

and the xaml is as follows

XML
<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>


Any help will be appreciated.

Regards,
Zafar
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900