tags:

views:

334

answers:

1

I have a custom Attached Property but the Accessors on never being accessed on databinding. Are theses accessors meant to be accessed everytime the attached property changes?

  public static readonly DependencyProperty CharacterColumnNumberProperty =
        DependencyProperty.RegisterAttached("CharacterColumnNumber", typeof(int), typeof(DragCanvas), new UIPropertyMetadata(1));

    public static int GetCharacterColumnNumber(UIElement uiElement)
    {
        if (uiElement != null)
            return (int)uiElement.GetValue(CharacterColumnNumberProperty);
        else return 0;
    }

    public static void SetCharacterColumnNumber(UIElement uiElement, int value)
    {
        if (uiElement != null)
        {
            uiElement.SetValue(CharacterColumnNumberProperty, value);
            DragCanvas.SetLeft(uiElement, value * 10);
        }
    }

XAML:

 <Setter Property="local:DragCanvas.CharacterColumnNumber" Value="{Binding Path=CharacterColumnNumber, Mode=TwoWay}" />
+2  A: 

No they are not. If you want to know when the internal property engine is changing these values you pass in a delegate for the PropertyChangedCallback parameter of the UIPropertyMetadata.

This delegate will be invoked each time the property is changed, whether it came through the CLR property or via changes internally in the dependency property engine (i.e. bindings).

Drew Marsh
Thanks for the answer, that makes sense.But as I understood attached properties is that one of there most frequent uses is to change the layout. Would the PropertyChangedCallback parameter give me the best option to change the Canvas.Left of the UIElement when the CharacterColumnNumber changes? ie: Canvas.Left = CharacterColumnNumber * 10.Just to reiterate, I would like to tie up the CharacterColumnNumber value to the location on the canvas.
Eli Perpinyal
After writing the last comment I thought of using a converter, maybe a better option? But if I did not want to use a converter would the above be the best way? Thanks.
Eli Perpinyal