PropertyChanged всегда нулевой

Я привязываю Datagrid к Observablecollection. Ниже приведен мой класс ObservableCollection. Но измененное свойство всегда равно NULL, и оно равно нулю даже после создания моего XAML таким образом. Пожалуйста, ведите меня в этом

Спасибо!

<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");
                }
            }
        }

0 ответов

В 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));
                }
            }
        }
Другие вопросы по тегам