Question

I want to draw lines in UserControls that are in a ListBox. The number of lines is a Dependency Property and is set via Xaml Style. If the property has changed, I want to draw the lines. But Setters aren't called, if property is changed by xaml. Xaml calls SetValue() itself. But I need to know, when this property is changed to call my function for drawing the lines. If I call this function in the constructor, the property isn't bound yet. Can anyone help me please.

Était-ce utile?

La solution

You can add PropertyChanged callback to your DependencyProperty declaration like

public static readonly DependencyProperty LineCountProperty = DependencyProperty.Register(
    "LineCount",
    typeof(int),
    typeof(Window),
    new FrameworkPropertyMetadata(
        0,
        new PropertyChangedCallback(OnLineCountChanged)
    )
);


private static void OnLineCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
   //Here you call you function on `d` by typecasting it into your class
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top