首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PropertyChanged始终为空

PropertyChanged始终为空
EN

Stack Overflow用户
提问于 2014-04-30 01:30:32
回答 1查看 106关注 0票数 1

我正在将一个Datagrid绑定到我的ObservableCollection类的Observablecollection.Below。但是更改的属性始终为NULL,即使在创建了这样的XAML之后,它也是null。请在这里指导我

谢谢!

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

代码语言:javascript
复制
   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");
                }
            }
        }
EN

回答 1

Stack Overflow用户

发布于 2019-05-22 16:07:17

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

代码语言:javascript
复制
<DataGridTextColumn Binding="{Binding Path = Sno} Header = "Serial" />

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

代码语言:javascript
复制
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));
                }
            }
        }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23371448

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档