简体   繁体   English

ItemsSource更改时,ComboBox不会更新

[英]ComboBox does not update when ItemsSource changes

I hav a ComboBox that is bound to a List<string> . 我有一个ComboBox绑定到List<string> When the List changes, the ComboBox does not, even though PropertyChanged was raised. 当列表更改时,即使提出了PropertyChanged,ComboBox也不会更改。 When debugging, I found out that the List Property is even read. 调试时,我发现甚至读取了List属性。

The error can be reproduced using the following code: 可以使用以下代码重现该错误:

XAML XAML

<Window x:Class="ComboBoxTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="90" Width="400">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ComboBox ItemsSource="{Binding Source, Mode=OneWay}"/>
        <Button Grid.Column="1" Content="add string" Command="{Binding}" CommandParameter="Add"/>
    </Grid>
</Window>

Code behind 后面的代码

using System.Windows;

namespace ComboBoxTest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new ViewModel();
        }
    }
}

ViewModel 视图模型

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;

namespace ComboBoxTest
{
    class ViewModel : INotifyPropertyChanged, ICommand
    {
        public ViewModel()
        {
            Source = new List<string>();
            Source.Add("Test1");
            Source.Add("Test2");
            Source.Add("Test3");
        }

        private List<string> _Source;

        public List<string> Source
        {
            get { return _Source; }
            set
            {
                _Source = value;
                OnPropertyChanged("Source");
            }
        }

        private void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event System.EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            if ((string)parameter == "Add")
            {
                Source.Add("New string");
                OnPropertyChanged("Source");
            }
        }
    }
}

Why isn't the ComboBox updating? 为什么ComboBox不更新?

The ComboBox does not update because it doesn't see any changes when it checks the List. ComboBox不会更新,因为它在检查列表时看不到任何更改。 The reference stays the same and the ComboBox is not informed about changes inside the List. 引用保持不变,并且不会向ComboBox通知列表内的更改。

Refactoring the code to use ObservableCollection instead of List will solve the problem, because ObservableCollection implements INotifyCollectionChanged , what is necessary to inform the View about Changes inside an Object. 重构代码以使用ObservableCollection而不是List可以解决此问题,因为ObservableCollection实现了INotifyCollectionChanged ,因此有必要向View通知对象内部的更改。

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

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