#1,153 – Handling Custom Dependency Properties that Affect Rendering
September 8, 2014 1 Comment
In our example of a custom “pie slice” shape, we defined StartAngle and EndAngle properties that dictate where the pie shape starts and ends. We then define how the shape is rendered by overriding the DefiningGeometry property.
One problem with the earlier code example was that the shape wasn’t automatically re-rendered if the start or end angles were changed. Since the values of these properties are used in rendering the shape, we want the shape to automatically redraw itself whenever either property changes.
We can force a re-render by specifying the AffectsRender flag when defining the metadata for the StartAngle and EndAngle properties. Below is updated code for defining these properties.
// Angle that arc starts at public double StartAngle { get { return (double)GetValue(StartAngleProperty); } set { SetValue(StartAngleProperty, value); } } // DependencyProperty - StartAngle private static FrameworkPropertyMetadata startAngleMetadata = new FrameworkPropertyMetadata( 0.0, // Default value FrameworkPropertyMetadataOptions.AffectsRender, null, // Property changed callback new CoerceValueCallback(CoerceAngle)); // Coerce value callback public static readonly DependencyProperty StartAngleProperty = DependencyProperty.Register("StartAngle", typeof(double), typeof(PieSlice), startAngleMetadata); // Angle that arc ends at public double EndAngle { get { return (double)GetValue(EndAngleProperty); } set { SetValue(EndAngleProperty, value); } } // DependencyProperty - EndAngle private static FrameworkPropertyMetadata endAngleMetadata = new FrameworkPropertyMetadata( 90.0, // Default value FrameworkPropertyMetadataOptions.AffectsRender, null, // Property changed callback new CoerceValueCallback(CoerceAngle)); // Coerce value callback public static readonly DependencyProperty EndAngleProperty = DependencyProperty.Register("EndAngle", typeof(double), typeof(PieSlice), endAngleMetadata);
Pingback: Dew Drop – September 8, 2014 (#1850) | Morning Dew