3

I made an usercontrol and it works great, but when I put two instances of this control to one window, only the last of them works. I tried to find solution and I realized, that dependency properties are shared, but I dont know how to get it work.

Here is my dependency property:

    public double AnimatingVerticalOffset
    {
        get { return (double)GetValue(AnimatingVerticalOffsetProperty); }
        set { SetValue(AnimatingVerticalOffsetProperty, value); }
    }

    public static readonly DependencyProperty AnimatingVerticalOffsetProperty;

    static ListChooser()
    {
        ListChooser.AnimatingVerticalOffsetProperty =
                   DependencyProperty.Register("AnimatingVerticalOffset", typeof(double), typeof(ListChooser), new UIPropertyMetadata(OnAnimationVerticalOffsetChanged));
    }
JanSkalicky
  • 1,007
  • 1
  • 6
  • 10
  • @user: Can you explain more about why your dependency properties are "shared?" The code you've posted looks correct and without more of a sample I'm not sure what problem you're seeing. – Dan Puzey Feb 10 '11 at 09:54
  • Hi, here is my problem: When I start window with more instance of this component, only last created instance changes dependency property. Rest of them see the value, which is set by last instance, but cannot set this property. There is no exception or something else it only doesnt work. – JanSkalicky Feb 11 '11 at 10:43
  • I'm sorry, ma fault. Problem was at the other place, I already solve it, but it was really strange. – JanSkalicky Sep 21 '11 at 11:46

1 Answers1

2

The dependency property itself must be static with no ties to one single instance. And that applies for its callbacks too (OnAnimationVerticalOffsetChanged in your case) - these must be static methods (don't worry, the object instance is passed via its parameter, you just have to do some type casting to ensure the object is the type you are working with).

You should use static initializer to initialize DP, the method you used (initializing in constructor) works, but the DP will overwrite for each instance.

See this question for deeper explanation.

EDIT:

Corrected code:

public double AnimatingVerticalOffset
{
    get { return (double)GetValue(AnimatingVerticalOffsetProperty); }
    set { SetValue(AnimatingVerticalOffsetProperty, value); }
}

public static readonly DependencyProperty AnimatingVerticalOffsetProperty =
               DependencyProperty.Register("AnimatingVerticalOffset", typeof(double), typeof(ListChooser), new UIPropertyMetadata(OnAnimationVerticalOffsetChanged));

static ListChooser()
{
}

If the callback is not static, you will get compile error (=> you have to make it static).

EDIT:

Remember, the DP definition is static, not the property's value itself! DPs work exactly just like any other property, it just has some extra features: value inhertiance, bidnings, animation...

Community
  • 1
  • 1
Matěj Zábský
  • 16,909
  • 15
  • 69
  • 114
  • I think i understand to this, but there must be way to do that. What about FontSize property on textbox. How is it done? I need to animate this property and therefore I'm searching in dependency properties. – JanSkalicky Feb 09 '11 at 15:43
  • FontSize is defined just like I posted. I edited the post with an explanation. And don't worry, just try what I posted. It works :) – Matěj Zábský Feb 09 '11 at 15:47
  • The OP is initializing in a static constructor - there shouldn't be anything wrong with that. A static constructor isn't called per-instance. – Dan Puzey Feb 09 '11 at 16:32