我在模板列中有一个wpf datagridview和combobox。我不想显示已经为其他行选择的值,但是在执行此操作时,当前选中的项也会被删除。
模型如下
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;
_isDirty = true;
_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按如下方式填充列表
_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如下:
<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>
在viewmodel中,我有一个列表_lstTaxonomyFileds,我正在使用它填充模型中的TaxonomyList。该列表包含三个项目“AB”,“BC”和“CD”。现在,用户将第一行添加到网格中并在组合框中包含所有三个选项并选择“AB”现在添加另一行,现在在组合框中只有“BC”和“CD”,直到他从中取消选择“AB”第一行。
答案 0 :(得分:0)
不是一个理想的解决方案,但不知何故,我能够使它工作。移动了MetadataConfiguration类中的TaxonomyField属性,做了一些linq魔术并将其绑定到itemsource。