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
.
Implement INotifyPropertyChanged
in your class.
Specify a callback in the property metadata when you register the dependency property.
In the callback, raise the PropertyChanged
event.
Adding the callback:
public static DependencyProperty FirstProperty = DependencyProperty.Register(
"First",
typeof(string),
typeof(MyType),
new FrameworkPropertyMetadata(
false,
new PropertyChangedCallback(OnFirstPropertyChanged)));
Raising PropertyChanged
in the callback:
private static void OnFirstPropertyChanged(
DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
PropertyChangedEventHandler h = PropertyChanged;
if (h != null)
{
h(sender, new PropertyChangedEventArgs("Second"));
}
}