简体   繁体   English

如何使用虚线路径作为PropertyChangedEventHandler的属性名称?

[英]How can I use a dotted path as a property name of a PropertyChangedEventHandler?

How can I use a dotted path as a property name of a PropertyChangedEventHandler? 如何使用虚线路径作为PropertyChangedEventHandler的属性名称?

public class Person
{
    private int _age;
    public int Age
    {
        get { return _age;}
        set
        {
            _age = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

public partial class MyControl : UserControl, INotifyPropertyChanged
{
    public Person Person
    {
        get { return (Person)GetValue(PersonProperty); }
        set { SetValue(PersonProperty, value); }
    }

    public static DependencyProperty PersonProperty =
        DependencyProperty.Register("Person", typeof (Person), typeof (MyControl), null);

    private void someMethod()
    {
        OnPropertyChanged("Person.Age");
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    }   
}

<TextBox Text="{Binding Person.Age, Mode=TwoWay}"/>

But OnPropertyChanged("Person.Age") cannot resolve the symbol. 但是OnPropertyChanged(“ Person.Age”)无法解析该符号。

Is it possible to use a dotted path as a propertyName of OnPropertyChanged()? 是否可以将虚线路径用作OnPropertyChanged()的propertyName?

The Age setter, you should always call OnPropertyChanged("Age") . Age设置器,您应该始终调用OnPropertyChanged("Age")

INotifyPropertyChanged isn't meant to be used for sub-properties. INotifyPropertyChanged不适用于子属性。 You also don't need it on a UserControl, since dependency properties already provide notification. 您还不需要在UserControl上使用它,因为依赖项属性已经提供了通知。 Once you fix your OnPropertyChanged call in the Person class you should be fine. Person类中修复OnPropertyChanged调用后,就可以了。

You have a couple of options to fix the Person.Age setter: 您可以通过以下几种方式修复Person.Age设置器:

  1. Call OnPropertyChanged("Age") (and remove the = null in the OnPropertyChanged signature. 调用OnPropertyChanged("Age") (并在OnPropertyChanged签名中删除= null

  2. If you are targeting .NET 4.5 or later, the preferred solution is to change the Person.OnPropertyChanged signature to be OnPropertyChanged(string [CallerMemberName] propertyName = null) . 如果您面向.NET 4.5或更高版本,则首选解决方案是将Person.OnPropertyChanged签名更改为Person.OnPropertyChanged OnPropertyChanged(string [CallerMemberName] propertyName = null) Calling OnPropertyChanged() from the Age setter will then fill set propertyName to Age . 然后从Age设置器调用OnPropertyChanged()会将设置的propertyName填充为Age See the this blog post or the MSDN documentation for more details. 有关更多详细信息,请参见此博客文章MSDN文档

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

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