Search code examples
wpfxamlmvvmmvvm-lightpropertychanged

Detect when a OnPropertyChanged occurs


Here is a simplified example of what i need:
I have class A that have a property Name. the Name property is changed asynchronously and there is no way to know when the modification occurs.
In order to show the updated value of it in the view, I wired a propertychanged event in it and bind it with {Binding A.Name}. In the VM it works fine.
But in my case, there is a lot of custom properties that shouldn't be in the class A. I'm thinking once propertychanged is raised in class A, the Name property in AViewModel should get notified and raise the OnPropertyChanged too

is there any way to do so ?

C# :

public class A : BaseViewModel
{
    string name;
    public string Name
    {
        get { return name; }
        set { Set(()=> Name, ref name, value); }
    }
}

public class AViewModel : BaseViewModel
{
    A a;
    public A A 
    {
        get { return a; }
        set { Set(()=> A, ref a, value); }
    }
    public string Name
    {
        get { return A.Name; }
        set { Set(()=> Name, ref A.Name, value); }
    }
}

XAML :

<TextBox Text="{Binding Name}" />

Solution

  • The class A must have a classic C# event, for example, so the AViewModel can subscribe it.