c# - 当 ItemsSource 更改 MVVM 时,Combobox SelectedItem 不会更新
问题描述
我在 mainviewmodel 中定义了 Aperture 的 OvserableCollection
主视图模型
public ObservableCollection<LookupItemViewModel> Apertures { get; }
public void LoadAperturesCollection()
{
Apertures.Clear();
var items = _apertureTableListService.GetAllApertures();
Apertures.Add(new LookupItemViewModel(0, null, true));
foreach (var item in items)
{
Apertures.Add(new LookupItemViewModel(item.Id, item.DisplayName, item.Active));
}
SessionViewModel.LoadActiveCollection(Apertures, nameof(Apertures));
}
我在 editviewmodel 中有一个 ActiveApertures 的 ObservableCollection - 这是 Apertures 的一个集合,其字段 Active 设置为 true
编辑视图模型
public ObservableCollection<LookupItemViewModel> ActiveApertures { get; }
private void LoadActiveAperturesCollection(ObservableCollection<LookupItemViewModel> collection)
{
ActiveApertures.Clear();
foreach (var item in collection)
{
if (item.Active)
{
ActiveApertures.Add(new LookupItemViewModel(item.Id, item.DisplayName, item.Active));
}
}
}
Exposure 表具有 Aperture 属性,它是 Aperture 表的外键
在 exposureeditview 中是一个带有 ActiveApertures 的 ItemsSource 的组合框
曝光编辑视图
<ComboBox
x:Name="comboBoxExposureDataAperture"
Grid.Column="3"
Grid.Row="5"
DisplayMemberPath="DisplayName"
IsSynchronizedWithCurrentItem="True"
IsTextSearchEnabled="True"
IsTextSearchCaseSensitive="True"
ItemsSource="{Binding ActiveApertures}"
SelectedValue="{Binding Exposure.ApertureId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="Id"
Style="{DynamicResource ComboboxData}"
VerticalAlignment="Bottom"
Width="110"
Margin="0,0,0,5"/>
如果我编辑 Aperture,Apertures 集合会更新,ActiveApertures 集合也会更新,但组合框中的 SelectedItem 现在为空。所有的视图模型都实现了 INotifyPropertyChanged()。
我一直在搜索互联网,但我还没有找到与此问题匹配的解决方案。任何建议,将不胜感激。
编辑 1
private LookupItemViewModel selectedRecord;
public LookupItemViewModel SelectedRecord
{
get { return selectedRecord; }
set
{
if(value != selectedRecord)
{
selectedRecord = value;
OnPropertyChanged();
}
}
}
<ComboBox
x:Name="comboBoxExposureDataAperture"
Grid.Column="3"
Grid.Row="5"
DisplayMemberPath="DisplayName"
IsSynchronizedWithCurrentItem="True"
IsTextSearchEnabled="True"
IsTextSearchCaseSensitive="True"
ItemsSource="{Binding ActiveApertures}"
SelectedItem="{Binding SelectedRecord}"
SelectedValue="{Binding Exposure.ApertureId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="Id"
Style="{DynamicResource ComboboxData}"
VerticalAlignment="Bottom"
Width="110"
Margin="0,0,0,5"/>
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
编辑2
public void LoadAperturesCollection()
{
Apertures.Clear();
var items = _apertureTableListService.GetAllApertures();
Apertures.Add(new LookupItemViewModel(0, null, true));
foreach (var item in items)
{
Apertures.Add(new LookupItemViewModel(item.Id, item.DisplayName, item.Active));
}
SessionViewModel.LoadActiveCollection(Apertures, nameof(Apertures));
}
private void LoadActiveAperturesCollection(ObservableCollection<LookupItemViewModel> collection)
{
ActiveApertures.Clear();
foreach (var item in collection)
{
if (item.Active)
{
ActiveApertures.Add(new LookupItemViewModel(item.Id, item.DisplayName, item.Active));
}
}
}
解决方案
我找到了一种使用 AfterDataSaved 事件更新 SelectedItem 的方法。
感谢所有试图提供帮助的人。
推荐阅读
- java - 在 Java 中通过控制台编辑给定的字符串
- google-maps - Flutter Maps:如何更改标记点击上的标记图标
- string - 如果包含特定字符串,则阻止 URL
- java - 在 Lucene 中使用 CharTermAttribute 显示整行
- r - R - 从格子改变 bwplot 上的箱线图
- java - 出现错误在范围内无法访问折线类型的封闭实例
- java - 如何管理 ThreadPoolTaskExecutor 来阻止一些请求
- variables - 从 Azure Devops 构建管道上的 Dockerfile 访问构建变量
- reactjs - 如何在 react native 的透明视图上创建自定义 UI?
- ajax - 在谷歌浏览器中如何禁用询问“电话呼叫”的弹出框