WPF Dependency Property not working
.net, c#, vb.net, wpf, xaml
Solution
Try this..
public string MyCustomProperty
{
get
{
return (string)GetValue(MyCustomPropertyProperty);
}
set
{
SetValue(MyCustomPropertyProperty, value);
}
}
// Using a DependencyProperty as the backing store for MyCustomProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyCustomPropertyProperty =
DependencyProperty.Register("MyCustomProperty", typeof(string), typeof(TargetCatalogControl), new UIPropertyMetadata(MyPropertyChangedHandler));
public static void MyPropertyChangedHandler(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
// Get instance of current control from sender
// and property value from e.NewValue
// Set public property on TaregtCatalogControl, e.g.
((TargetCatalogControl)sender).LabelText = e.NewValue.ToString();
}
// Example public property of control
public string LabelText
{
get { return label1.Content.ToString(); }
set { label1.Content = value; }
}
Problem
I have a custom Dependency Property defined like so: ``` public static readonly DependencyProperty MyDependencyProperty = DependencyProperty.Register( "MyCustomProperty", typeof(string), typeof(MyClass)); private string _myProperty; public string MyCustomProperty { get { return (string)GetValue(MyDependencyProperty); } set { SetValue(MyDependencyProperty, value); } } ``` Now I try set that property in XAML ``` <controls:TargetCatalogControl MyCustomProperty="Boo" /> ``` But the setter in DependencyObject never gets hit! Although it does when I change the property to be a regular property and not a Dep Prop