操纵ItemsSource但保留选定的值

时间:2019-07-05 09:57:14

标签: c# wpf mvvm combobox

在我的应用程序中,我有一个DataGrid,用户可以通过按Button来添加行。通过按下按钮,将在绑定{{1}的PlaceholderItem上添加ObservableCollection<PlaceholderItem>的新实例。

视图中DataGrid的定义如下:

DataGrid

因此<DataGrid ItemsSource="{Binding PlaceholderItems}" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" Background="White" Grid.Row="1" MinHeight="70" MaxHeight="200" Margin="7,0" > <DataGrid.Columns> <DataGridTemplateColumn Header="Placeholder-Name" Width="*"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox IsEditable="True" VerticalAlignment="Center" Margin="2" ItemsSource="{Binding DataContext.AvailablePlaceholders, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" DisplayMemberPath="PlaceholderName" Validation.ErrorTemplate="{StaticResource ComboBoxErrorTemplate}"> <ComboBox.Text> <Binding Path="PlaceholderName" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" > <Binding.ValidationRules> <local:PlaceholderValidationRule ValidationStep="UpdatedValue"> <local:PlaceholderValidationRule.PlaceholderValidationRuleParamData> <local:PlaceholderValidationRuleParamData UsedPlaceholders="{Binding Source={StaticResource proxy}, Path=Data.PlaceholderItems}" AvailablePlaceholders="{Binding Source={StaticResource proxy}, Path=Data.AvailablePlaceholders}"/> </local:PlaceholderValidationRule.PlaceholderValidationRuleParamData> </local:PlaceholderValidationRule> </Binding.ValidationRules> </Binding> </ComboBox.Text> </ComboBox> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTemplateColumn Header="Placeholder-Value" Width="*"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBox VerticalAlignment="Center" Validation.ErrorTemplate="{StaticResource PlaceholderItemValueErrorTemplate}"> <TextBox.Text> <Binding Path="PlaceholderValue" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True"> <!--<Binding.ValidationRules> <local:PlaceholderValueValidationRule/> </Binding.ValidationRules>--> </Binding> </TextBox.Text> </TextBox> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> 的ItemsSource称为PlaceholderItems。 DataGrid的每一行都由两列组成。第一列是DataGrid,其中ComboBox的名称是可选的。第二列是PlaceholderItem

的值

PlaceholderItem类只有两个实现INotifyPropertyChanged的字符串属性。

每行第一列中的PlaceholderItem绑定到ComboBox。因此,用户可以从ObservableCollection<PlaceholderItem>中选择一项,并将ComboBox的值写入第二列中的PlaceholderItem

Therfor我在ViewModel中使用以下代码:

TextBox

一切正常。

现在的问题:

如果用户添加了新行并从public ObservableCollection<PlaceholderItem> PlaceholderItems { get { if (placeholderItems == null) { placeholderItems = new ObservableCollection<PlaceholderItem>(); placeholderItems.CollectionChanged += PlaceholderItemsOnCollectionChanged; } return placeholderItems; } } private void PlaceholderItemsOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (e.NewItems != null) { foreach (PlaceholderItem placeholderItem in e.NewItems.OfType<PlaceholderItem>()) { placeholderItem.PropertyChanged += PlaceholderItemOnPropertyChanged; } } if (e.OldItems != null) { foreach (PlaceholderItem placeholderItem in e.OldItems.OfType<PlaceholderItem>()) { placeholderItem.PropertyChanged -= PlaceholderItemOnPropertyChanged; } } } private void PlaceholderItemOnPropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "PlaceholderName") { PlaceholderItem targetAvailablePlaceholderItem = AvailablePlaceholders.FirstOrDefault(p => p.PlaceholderName == ((PlaceholderItem)sender).PlaceholderName); if (targetAvailablePlaceholderItem != null) { ((PlaceholderItem)sender).PlaceholderValue = targetAvailablePlaceholderItem.PlaceholderValue; } } } 中选择了一个项目,我想从AvailablePlaceholders-Collection中删除选定的项目。这样用户就不能再在其他行中选择它了。

我已经在PlaceholderItemOnProertyChanged-Method中尝试了以下方法:

ComboBox

该项目已从集合中删除,但 if (e.PropertyName == "PlaceholderName") { PlaceholderItem targetAvailablePlaceholderItem = AvailablePlaceholders.FirstOrDefault(p => p.PlaceholderName == ((PlaceholderItem)sender).PlaceholderName); if (targetAvailablePlaceholderItem != null) { ((PlaceholderItem)sender).PlaceholderValue = targetAvailablePlaceholderItem.PlaceholderValue; AvailablePlaceholders.Remove(targetAvailablePlaceholderItem); } ((PlaceholderItem)sender).ForcePropertyChanged(); } 中的值现在为空。

是否可以从ComboBox的ItemsSource中删除一项,但是ComboBox的文本属性保留该值?

0 个答案:

没有答案