简体   繁体   English

INotifyPropertyChanged的PropertyChanged成员始终为null

[英]PropertyChanged member of INotifyPropertyChanged always null

I have a class as follows 我有一个课程如下

public partial class Configuration : INotifyPropertyChanged
{
    private bool _isToolTips;
    public bool IsToolTips
    {
        get { return _isToolTips; }
        set { Set(this, "IsToolTips", ref _isToolTips, value, PropertyChanged); }
    }

    #region INotifyPropertyChanged functionality

    public static void Set<T>(object owner, string propName, ref T oldValue, T newValue,
        PropertyChangedEventHandler eventHandler)
    {
        // make sure the property name really exists
        if (owner.GetType().GetProperty(propName) == null)
        {
            throw new ArgumentException("No property named ‘" +
                propName + "‘ on " + owner.GetType().FullName);
        }

        // we only raise an event if the value has changed
        if (Equals(oldValue, newValue)) return;
        oldValue = newValue;
        if (eventHandler != null)
        {
            eventHandler(owner, new PropertyChangedEventArgs(propName));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void notifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    #endregion INotifyPropertyChanged functionality
}

This class is compiled in a separate assembly. 此类在单独的程序集中编译。 I then add it to my MainWindow constructor as follows [edit] 然后我将它添加到我的MainWindow构造函数中,如下所示[编辑]

    public MainWindow()
    {
        initializeAttributes();
        InitializeComponent();
        CurrentConfig = new Configuration
        {
            IsToolTips = true
        };
        DataContext = CurrentConfig;
        composeModules();
    }

[end edit] and in my XMAL I have this [edit] [结束编辑]在我的XMAL中我有[编辑]

<MenuItem Name="ToolTips"
    Header="Tool Tips"
    IsCheckable="True"
    IsChecked="{Binding Source=CurrentConfig,
                        Path=IsToolTips,
                        Mode=TwoWay}"
    Click="onToolTipsClick">
</MenuItem>

[end edit] The problem is that the "PropertyChanged" member of my Configuration class is always set to null. [end edit]问题是我的Configuration类的“PropertyChanged”成员总是设置为null。 Where am I going wrong? 我哪里错了?

The problem is that you're initializing your CurrentConfig too late: 问题是你太晚了你的CurrentConfig初始化:

// Setting data context here...
DataContext = CurrentConfig;
InitializeComponent();

// This makes a new instance, but does not update the data context!
CurrentConfig = new Configuration
    {
        IsToolTips = true
    };

I suspect you want to change this around: 我怀疑你想改变这个:

InitializeComponent();    
CurrentConfig = new Configuration
    {
        IsToolTips = true
    };

// Set this AFTER you construct your CurrentConfig
DataContext = CurrentConfig;

Also, your binding is setup with Source=CurrentConfiguration , but your property name seems to be CurrentConfig . 此外,您的绑定是使用Source=CurrentConfiguration设置的,但您的属性名称似乎是CurrentConfig You may need to adjust your data context or binding path. 您可能需要调整数据上下文或绑定路径。 With the DataContext set as displayed, you can likely remove the source, as you want the binding to bind to itself. 将DataContext设置为显示后,您可以删除源,因为您希望绑定绑定到自身。

Source=CurrentConfiguration sets the Source to the string "CurrentConfiguration" , which obviously won't have that property. Source=CurrentConfigurationSource设置为字符串"CurrentConfiguration" ,显然不具有该属性。

Debug your binding and see the references if you don't know enough about them. 如果您对它们不够了解, 调试绑定查看引用

Remove the null check. 删除空检查。 When you normally implement an INotifyProperty you don't check for it. 通常实现INotifyProperty时,不要检查它。

public string _PhoneNumber
public string PhoneNumber
{
    get
    {
        return _PhoneNumber;
    }

    set
    {
        if (value != PhoneNumber)
        {
            _PhoneNumber = value;
            NotifyPropertyChanged("PhoneNumber");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM