首页 > 解决方案 > 当 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));
                }
            }
        }

标签: c#xamlmvvmcomboboxobservablecollection

解决方案


我找到了一种使用 AfterDataSaved 事件更新 SelectedItem 的方法。

感谢所有试图提供帮助的人。


推荐阅读