【问题标题】:PropertyChanged event is null after changing values inside property更改属性内的值后,PropertyChanged 事件为空
【发布时间】:2019-03-26 21:30:20
【问题描述】:

更新我的 ObservableCollection 属性后,我在更新 UI 时遇到问题。

我的构造函数:

public ObservableCollection<Positions> Positions { get; set; }

        public BindableBase BindableBase { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            Positions = new ObservableCollection<Positions>();
            BindableBase = new BindableBase();

            Positions.Add(new Positions
            {
                Position1 = "Red",
                Position2 = "Red",
                Position3 = "Red",
                Position4 = "Gray",
                Position5 = "Green",
                Position6 = "Green",
                Position7 = "Green",
            });

            this.DataContext = this;
        }

这是更改 ObservableCollection 内部值的 if 语句,然后我用我的属性名称调用 RaisePropertyChanged 事件。因为我没有包含所有代码,所以假设它在 if 语句中。

if (Positions[0].Position4 == "Gray")
            {
                Positions[0].Position1 = "Red";
                Positions[0].Position2 = "Red";
                Positions[0].Position3 = "Gray";
                Positions[0].Position4 = "Red";
                Positions[0].Position5 = "Green";
                Positions[0].Position6 = "Green";
                Positions[0].Position7 = "Green";

                BindableBase.RaisePropertyChanged("Positions");
            }

这是我的 RaisePropertyChanged 代码:

public class BindableBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

问题是,如果我调用 RaisePropertyChanged 事件,它不会更新 UI。经过一些调试后,我发现 PropertyChanged 的​​值为 NULL,因此即使我进行了更改,它也不会更新。有没有人解决这个问题?

【问题讨论】:

    标签: c# wpf user-interface data-binding inotifypropertychanged


    【解决方案1】:

    PropertyChanged 始终为 null,因为没有实际使用 BindableBase 属性值作为源对象的绑定(源对象是 MainWindow,DataContext = this)。

    幸运的是,这里不需要 BindableBase。 Positions 是一个 ObservableCollection,不需要任何额外的属性更改通知。如果需要,Positions 必须是 BindableBase 的一个属性,否则触发 PropertyChanged 事件是没有意义的。

    替换

    Positions[0].Position1 = "Red";
    Positions[0].Position2 = "Red";
    Positions[0].Position3 = "Gray";
    Positions[0].Position4 = "Red";
    Positions[0].Position5 = "Green";
    Positions[0].Position6 = "Green";
    Positions[0].Position7 = "Green";
    

    通过

    Positions[0] = new Positions
    {
        Position1 = "Red",
        Position2 = "Red",
        Position3 = "Gray",
        Position4 = "Red",
        Position5 = "Green",
        Position6 = "Green",
        Position7 = "Green",
    };
    

    除此之外,将null check 和PropertyChanged.Invoke 的组合替换为:

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

    【讨论】:

    猜你喜欢
    • 2014-04-08
    • 2011-02-08
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 2011-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多