I have a viewmodel that is tagged with [NotifyPropertyChanged]
. The properties are of course bound on input controls, like textboxes. I need to know, that the model's property was changed because of an input.
How can I catch this event?
I have a viewmodel that is tagged with [NotifyPropertyChanged]
. The properties are of course bound on input controls, like textboxes. I need to know, that the model's property was changed because of an input.
How can I catch this event?
If a class decorated by NotifyPropertyChanged implements INotifyPropertyChanged directly, then PostSharp requires that there is a method with signature:
void OnPropertyChanged(string propertyName)
This method has to raise PropertyChanged event explicitly. Working example could look like this:
[NotifyPropertyChanged]
public class OsModel : INotifyPropertyChanged
{
public int P1 { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Additional information could be found here.
Actually there was "no code", because Postsharp did the hard work, so I had to declare my class only with the tag:
[NotifyPropertyChanged]
public class OsModell // : ... interfaces only
{
...
}
Only thing for I answer my question is that it may be useful for someone else too to know, how to get event from this situation when you have no code, because postsharp does it compile-time. You can use reflexion runtime and attach your own event handler(s). You may think, that it kills the purpose of having Postsharp for this, but some cases (like mine was/is where I use a well-written parent class to derive from) you may find it more useful then calling OnPropertyChanged-like methods you write your own self.
So.. you can attach this only runtime, because in design time you got no code, because PS does the hard work. You can do something like this:
[NotifyPropertyChanged]
public class OsModell //...
{
public OsModell()
{
//...
#region feliratkozás helyi propertychangedre úgy, hogy még dizájn alatt nem létezik
//forrás: http://msdn.microsoft.com/en-us/library/ms228976(v=vs.100).aspx
{
EventInfo feliratkozasiSegedEventInfo = this.GetType().GetEvent("PropertyChanged");
Type propertyChangedEventhandlerTipusa = feliratkozasiSegedEventInfo.EventHandlerType;
MethodInfo segedAhhozAmitMajdAdattartalomValtozasahozCsatolok = this.GetType().GetMethod("megvaltozottAzAdattartalom", BindingFlags.NonPublic | BindingFlags.Instance);
Delegate d = Delegate.CreateDelegate(propertyChangedEventhandlerTipusa, this, segedAhhozAmitMajdAdattartalomValtozasahozCsatolok);
MethodInfo addHandler = feliratkozasiSegedEventInfo.GetAddMethod();
Object[] addHandlerArgs = { d };
addHandler.Invoke(this, addHandlerArgs);
}
#endregion
}
protected void megvaltozottAzAdattartalom(Object sender, EventArgs e)
{
//...
}
You may find the link in the code block useful.
If you derive from this class, you should use protected
, if not you also may use private
.