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
.
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.
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.
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.
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.
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.
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.
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.
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.
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"); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With