繁体   English   中英

当 ItemsSource 更改 MVVM 时,组合框 SelectedItem 不会更新

[英]Combobox SelectedItem doesn't update when ItemsSource changes MVVM

我在主视图模型中定义了一个 OvserableCollection of 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));
        }

我在编辑视图模型中有一个 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 表的外键

在曝光编辑视图中是一个带有 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));
                }
            }
        }

那么为什么不在Combobox绑定SelectItem呢?

如果您在 viewModel 中实现了INotifyPropertyChanged ,请创建一个名为SelectedRecord的 ActiveApertures 项示例并将其绑定到ComboBox 改变它,如下图所示

EditViewModel更改为:

public class EditViewModel : INotifyPropertyChanged
{
    public ObservableCollection<LookupItemViewModel> ActiveApertures { get; }
    private LookupItemViewModel _selectedRecord;
    public LookupItemViewModel SelectedRecord
    {
        get
        {
            return _selectedRecord;
        }
        set
        {
            if(value != _selectedRecord)
            {
                _selectedRecord = value;
                OnPropertyChanged("SelectedRecord");
            }
        }
    }
    /.............................................................
    /.............................................................
    /.............................................................
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }  
}
        

ComboBox更改为:

<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"
            SelectedItem="{Binding SelectedRecord}"
            Style="{DynamicResource ComboboxData}"
            VerticalAlignment="Bottom"
            Width="110"
            Margin="0,0,0,5"/>

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM