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.

有帮助吗?

解决方案

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
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top