2

I'd like to make a UserControl that I can reuse for the various buttons in my application. Is there a way to pass parameters to UserControls through XAML? Most of the buttons in my app will consist of two rectangles (one within the other) with some user specified colors. It will also possibly have an image. I'd like it to behave something like this this:

<Controls:MyCustomButton MyVarColor1="<hard coded color here>" MyVarIconUrl="<null if no icon or otherwise some URI>" MyVarIconX="<x coordinate of icon within button>" etc etc>

Then inside the button I would like to be able to use these values inside the XAML (assign the IconUrl to the source of the Icon, etc, etc.

Am I just thinking about this the wrong way or is there a way to do this? My purpose is to have less XAML code for all my buttons.

Thanks!

Joris Weimar
  • 4,783
  • 4
  • 33
  • 53

2 Answers2

5

Yes, you can access any property in a Control in xaml, but if you want to DataBind, Animate etc the properties in your UserControl have to be DependencyProperties.

example:

public class MyCustomButton : UserControl
{
    public MyCustomButton()
    {
    }

    public Brush MyVarColor1
    {
        get { return (Brush)GetValue(MyVarColor1Property); }
        set { SetValue(MyVarColor1Property, value); }
    }

    // Using a DependencyProperty as the backing store for MyVarColor1.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MyVarColor1Property =
        DependencyProperty.Register("MyVarColor1", typeof(Brush), typeof(MyCustomButton), new UIPropertyMetadata(null));



    public double MyVarIconX
    {
        get { return (double)GetValue(MyVarIconXProperty); }
        set { SetValue(MyVarIconXProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MyVarIconX.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MyVarIconXProperty =
        DependencyProperty.Register("MyVarIconX", typeof(double), typeof(MyCustomButton), new UIPropertyMetadata(0));



    public Uri MyVarIconUrl
    {
        get { return (Uri)GetValue(MyVarIconUrlProperty); }
        set { SetValue(MyVarIconUrlProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MyVarIconUrl.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MyVarIconUrlProperty =
        DependencyProperty.Register("MyVarIconUrl", typeof(Uri), typeof(MyCustomButton), new UIPropertyMetadata(null));

}

xaml:

<Controls:MyCustomButton MyVarColor1="AliceBlue" MyVarIconUrl="myImageUrl" MyVarIconX="60" />
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110
0

If you're talking about passing constructor parameters in XAML, this isn't possible. You'll have to set them through properties after the object initialized, or you need to instanciate it through code.

There's a similar question here: Naming user controls without default constructors in XAML

Community
  • 1
  • 1
Joe
  • 2,496
  • 1
  • 22
  • 30