Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Raise Property Changed events on a Dependency Property?

Tags:

binding

wpf

I have a control with two properties. One is a DependencyProperty, the other is an "alias" to the first one. How do I raise the PropertyChanged event for the second one (the alias) when the first one is changed.

NOTE: I'm using DependencyObjects, not INotifyPropertyChanged (tried that, didn't work because my control is a ListVie sub-classed)

Something like this.....

protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) {     base.OnPropertyChanged(e);     if (e.Property == MyFirstProperty)     {         RaiseAnEvent( MySecondProperty ); /// what is the code that would go here?     }     } 

If I were using an INotify I could do like this...

public string SecondProperty {     get     {         return this.m_IconPath;     } }  public string IconPath {     get     {         return this.m_IconPath;     }     set     {         if (this.m_IconPath != value)         {             this.m_IconPath = value;         this.SendPropertyChanged("IconPath");         this.SendPropertyChanged("SecondProperty");         }     } } 

Where can I raise PropertyChanged events on multiple properties from one setter? I need to be able to do the same thing, only using DependencyProperties.

like image 838
Muad'Dib Avatar asked Mar 19 '10 20:03

Muad'Dib


People also ask

How do you raise property to change events?

RaisePropertyChanged("User"); From MSDN: The PropertyChanged event can indicate all properties on the object have changed by using either null or String. Empty as the property name in the PropertyChangedEventArgs.

Why is dependency property static?

DependencyProperty has to be static (Class level) because when we create multiple objects of the class which has that property and want to refer the default value for that property the value has to come from that static instance of DependencyProperty.

Why do we need dependency properties?

The purpose of dependency properties is to provide a way to compute the value of a property based on the value of other inputs, such as: System properties, such as themes and user preference. Just-in-time property determination mechanisms, such as data binding and animations/storyboards.

Which is dependency property in this code?

A Dependency Property is a property whose value depends on the external sources, such as animation, data binding, styles, or visual tree inheritance. Not only this, but a Dependency Property also has the built-in feature of providing notification when the property has changed, data binding and styling.

How to change the value of a dependency property?

According to your code, the dependency property is registered as "ReadOnly", which is not supposed to change. You can use DependencyProperty.Register method to register this dependency property if its value could be changed. Your code of adding value change callback to the property look fine.

What does the propertychanged event indicate?

The PropertyChanged event can indicate all properties on the object have changed by using either null or String.Empty as the property name in the PropertyChangedEventArgs. Show activity on this post.

How to get the property changed event from another class?

So in your case: The PropertyChanged event can indicate all properties on the object have changed by using either null or String.Empty as the property name in the PropertyChangedEventArgs. Show activity on this post. You can invoke a property changed event from another class.

How to catch changes in entieis property changed?

Entities already implement the PropertyChanged event since they implement System.ComponentModel.INotifyPropertyChanged. If you want to catch changes to your entieis, you can just subscribe to that.


1 Answers

I ran into a similar problem where I have a dependency property that I wanted the class to listen to change events to grab related data from a service.

public static readonly DependencyProperty CustomerProperty =      DependencyProperty.Register("Customer", typeof(Customer),         typeof(CustomerDetailView),         new PropertyMetadata(OnCustomerChangedCallBack));  public Customer Customer {     get { return (Customer)GetValue(CustomerProperty); }     set { SetValue(CustomerProperty, value); } }  private static void OnCustomerChangedCallBack(         DependencyObject sender, DependencyPropertyChangedEventArgs e) {     CustomerDetailView c = sender as CustomerDetailView;     if (c != null) {         c.OnCustomerChanged();     } }  protected virtual void OnCustomerChanged() {     // Grab related data.     // Raises INotifyPropertyChanged.PropertyChanged     OnPropertyChanged("Customer"); } 
like image 139
Brett Ryan Avatar answered Sep 21 '22 15:09

Brett Ryan