【发布时间】:2021-09-26 16:11:38
【问题描述】:
我在主视图模型中定义了一个 OvserableCollection 的 Aperture
主视图模型
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));
}
}
}
曝光表有一个 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));
}
EDIT2
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));
}
}
}
【问题讨论】:
标签: c# xaml mvvm combobox observablecollection