none
How do I reference a dependency property in XAML from the UserControl it's defined in?

    Question

  • Suppose you define a dependency property in a UserControl and suppose it's bound to a view-model.

    In the XAML of the UserControl, how do I reference that property in order to set it to a property on the view-model? Please don't suggest using code

    The problem is the top-level element is <UserControl> and therefore the designer cannot access properties in your UserControl-derived class



    • Edited by PhilipYork Thursday, November 8, 2012 12:56 AM
    Thursday, November 8, 2012 12:55 AM

Answers

  • Hi PhilpYork,

    If you do set the value of the UserControl's property in its xaml file, the UserControl will possibly break because the user of the UserControl can give that property a totally different value. So the value will no long be the one you expect it to be.

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, November 9, 2012 7:07 AM
    Moderator

All replies

  • Hi,

    Take a look at below link, it explain how to define Dependency Property for UserControl and how to set that dependency property from parent control.

    http://www.codeproject.com/Articles/224230/Exploring-the-use-of-Dependency-Properties-in-User

    Thanks,

    Rajnikant

    Thursday, November 8, 2012 4:50 AM
  • I'm not trying to set it from a parent / child control - I'm trying to set it from the XAML of the same UserControl it's defined inside of
    Thursday, November 8, 2012 1:42 PM
  • Hi,

    Let's see if I'm getting this straight:

    You have a custom usercontrol with a custom dependency property. Now in markup you want to do something like this:

    <UserControl x:Name="foo" MyDependencyPropertyInCustomUC="{Binding SomeValueinVM}" />

    Why would you want to do that? Is it because you have a series of similar usercontrols and you want to change the active usercontrol at runtime? Sort of like working with frames?

    If so, you can create abstract usercontrol, or an interface with the common properties. In Xaml you'ld declare the abstract usercontrol instead of the general usercontrol.

    A second option might be to use the RegisterAttached. Example:

    public static readonly DependencyProperty IsSpinningProperty = DependencyProperty.RegisterAttached(
      "IsSpinning",
      typeof(Boolean),
      typeof(Spinner)
    );
    public static void SetIsSpinning(UIElement element, Boolean value)
    {
        element.SetValue(IsSpinningProperty , value);
    }
    public static Boolean GetIsSpinning (UIElement element)
    {
        return (Boolean)element.GetValue(IsSpinningProperty );
    }
    <UserControl x:Name="bar" local:Spinner.IsSpinning="{Binding IsSpinningViewModelProperty}" />

    Where local is the namespace of the spinner class.

    For more info on registerattached see this link.


    Sincerely, Brecht

    Thursday, November 8, 2012 4:26 PM
  • Why is it wrong to do that, just out of curiosity? In a view with a view-model you have bindings in the view to properties on the view-model, why would it be wrong to bind a dependency property the user-control itself to a view-model property?

    Maybe I'm thinking about things the wrong way, as admittedly, I'm still new to MVVM but this is my intent:

    I have a reusable UserControl I want to use elsewhere and it has a view-model. This VM has a property which represents the internal state of the control

    If I put the UserControl in another window or control, I would think that I would bind to the dependency properties exposed by the control, wouldn't I? (rather than the view-model of the control, thus keeping its implementation hidden)

    Is this the wrong way to think about it? I've heard people tell me the view-model of the user control should be a property of the containing window / control's own view-model class, and that I should bind to the view model of the user control, rather than any of its dependency properties

    Thursday, November 8, 2012 10:20 PM
  • Hi PhilpYork,

    If you do set the value of the UserControl's property in its xaml file, the UserControl will possibly break because the user of the UserControl can give that property a totally different value. So the value will no long be the one you expect it to be.

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, November 9, 2012 7:07 AM
    Moderator
  • Hi PhilpYork,
     
    I am marking your issue as "Answered", if you have new findings about your issue, please let me know.


    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, November 22, 2012 7:06 AM
    Moderator