PropertyChanged始终为null

时间:2014-04-29 17:30:32

标签: inotifypropertychanged propertychanged

我将Datagrid绑定到Observablecollection.Below是我的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");
                }
            }
        }

1 个答案:

答案 0 :(得分: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
            { return sno1; }

            set
            {
                if (value != sno1)
                {
                    sno1= value;
                    OnPropertyChanged(nameof(Sno));
                }
            }
        }