【问题标题】:PropertyChanged is always nullPropertyChanged 始终为空
【发布时间】:2014-06-15 18:59:53
【问题描述】:

我将 Datagrid 绑定到 Observablecollection。下面是我的 ObservableCollection 类。 但是更改的属性始终为 NULL,即使在像这样制作我的 XAML 之后它也为 null。 请在这方面指导我

谢谢!

<DataGridTextColumn Binding="{Binding, Mode=TwoWay, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged} Header = "Serial" />

   public class itemobject 
    {
        public event PropertyChangedEventHandler PropertyChanged;

        // This method is called by the Set accessor of each property. 
        // The CallerMemberName attribute that is applied to the optional propertyName 
        // parameter causes the property name of the caller to be substituted as an argument. 
        private void NotifyPropertyChanged( String propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        int sno1;
        public int Sno
        {

            get
            { return sno1; }

            set
            {
                if (value != sno1)
                {
                    sno1= value;
                    NotifyPropertyChanged("Sno");
                }
            }
        }

【问题讨论】:

    标签: inotifypropertychanged propertychanged


    【解决方案1】:

    在 XAML 中,您应该指定确切更改的属性。

    <DataGridTextColumn Binding="{Binding Path = Sno} Header = "Serial" />
    

    和我一样,您需要创建 ViewModelBase 类。

    public abstract class ViewModelBase : INotifyPropertyChanged, IDisposable {
            public event PropertyChangedEventHandler PropertyChanged;
            public void OnPropertyChanged([CallerMemberName]string propertyName = null) {
                var handle = PropertyChanged;
                handle?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
            public virtual void Dispose() => PropertyChanged = null;
        }
    
    
    
    
    
     public class itemobject : ViewModelBase{
            int sno1;
            public int Sno{
                get => sno1;
                set{
                    if (value != sno1){
                        sno1= value;
                        OnPropertyChanged(nameof(Sno));
                    }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2010-12-03
      • 1970-01-01
      • 2011-10-13
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      相关资源
      最近更新 更多