1
votes

I have class deriving from TextBox to which I attach a dependency property of type point called position and in its set section I set the Canvas.Top and Canvas.Left property. Just to clarify, whenever the source property changes it calls the set section of the property correct? Because when my source updates the canvastop and canvasleft properties of the textbox don't get updated. Any help would be appreciated.

public static readonly DependencyProperty PositionProperty = DependencyProperty.Register("Position", typeof(Point), typeof(TextBox), new FrameworkPropertyMetadata(new Point(0, 0)));

    public Point Position
    {
        get { return (Point)GetValue(PositionProperty); }
        set
        {
            SetValue(PositionProperty, value);
            Canvas.SetLeft(this, value.X - this.Width / 2);
            Canvas.SetTop(this, value.Y - this.FontSize);
        }
    }

 this.TextBoxShape.SetBinding(TextBoxShape.PositionProperty, CreateConnectorBinding(this));

Where CreateConnectorBinding returns the mid point of an ellipse based on it Canvas.Top and Canvas.Left properties. But when the Ellipse's Canvas.Top and Canvas. Left properties get updated the position of the textbox is still not updated.

2

2 Answers

2
votes

Just to clarify, whenever the source property changes it calls the set section of the property correct?

No. This only happens when you call the property from code. The binding system bypasses the setter entirely.

If you need to do this, the proper way is to use Property Changed Callbacks registered in the DP's Metadata.

1
votes

When the PositionProperty value changes via the binding, it does not use the setter. You need to add a PropertyChangeCallback to the FrameworkPropertyMetadata on your DependencyProperty registration:

public static readonly DependencyProperty PositionProperty =
    DependencyProperty.Register("Position", typeof(Point), typeof(TextBox),
    new FrameworkPropertyMetadata(new Point(0, 0), PositionChanged));  

private static void PositionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    YourControl control = (YourControl)d;
    Canvas.SetLeft(d, d.Position.X - d.Width / 2);    
    Canvas.SetTop(d, d.Position.Y - d.FontSize);
}