34

I have created my own UserControl "ClockControl", which I initialize through the main window's XAML.

The only problem is that I have to pass a parameter to the constructor of the clock control, and I have no clue of I how I can do that.

This works if I have no parameters:

<myControl:ClockControl></myControl:ClockControl>

But how can I pass a parameter doing this?

Here is the constructor:

public ClockControl(String city)
    {
        InitializeComponent();
        this.initController();
        ......
        .....
    }

Thanks in advance.

1

4 Answers 4

66

Your constructor:

public ClockControl(String city)
{
    InitializeComponent();
    this.initController();
    //...
}

First of all, if you want to use ClockControl from XAML, then you need a default constructor, means a constructor which doesn't take any parameter. So the above constructor is not going to work.

I would suggest you to define a property with name City, preferably dependency property, and then use it from XAML. Something like this:

public class ClockControl: UserControl
    {
        public static readonly DependencyProperty CityProperty = DependencyProperty.Register
            (
                 "City", 
                 typeof(string), 
                 typeof(ClockControl), 
                 new PropertyMetadata(string.Empty)
            );

        public string City
        {
            get { return (string)GetValue(CityProperty); }
            set { SetValue(CityProperty, value); }
        }

        public ClockControl()
        {
            InitializeComponent();
        }
        //..........
}

Then you can write this in XAML:

<myControl:ClockControl City="Hyderabad" />

Since City is a dependency property, that means you can even do Binding like this:

<myControl:ClockControl City="{Binding Location}" />

Hope, that solves your problem!

2
  • At last! Someone knows what I wanted...!
    – Vahid
    Jan 10, 2015 at 9:42
  • 1
    @Nawaz, what John wants and what John needs may or may not be the same. It may be better to use a different workaround because we have x:Reference attribute in XAML now. Would you mind reviewing? Proper constructor parameter passing is not yet available in XAML as far as I know. Dec 6, 2016 at 14:35
4

This is done with the use of DependencyProperty's, however not via the constructor. Just by adding properties to the control itself and using them from the code-behind.

Have a read of the following in regards to DependencyProperty's:

As a visual note, what this will allow you to do is the following, and then use it in the code-behind:

<myControl:ClockControl City="New York"></myControl:ClockControl>
8
  • 1
    Using property in constructor? When 'New York' value will be set?
    – Snowbear
    Jan 26, 2011 at 9:53
  • @Snowbear - That's not at all what I said. Was your vote the downvote? Jan 26, 2011 at 11:19
  • @Kyle Rozendo - yes, it was mine downvote. Your last line in .ctor code seems wrong to me, that's why I downvoted your answer and upvoted previous one.
    – Snowbear
    Jan 26, 2011 at 13:36
  • @Snowbear - What doesn't it seem right? It's pseudo-real code on how to do it. I have three links explaining how to implement it 100%, as well as in understanding the back-end of how dependency properties work. I do not see how this justifies a downvote. Jan 26, 2011 at 14:03
  • @Kyle Rozendo - usage of 'this.initControl(City)' with comment 'passes New York into this' doesn't seem right, because it will not pass 'New York' value if invoked from constructor. It should be invoked from DP callback instead of constructor. Do you agree?
    – Snowbear
    Jan 26, 2011 at 14:37
1

x:Arguments directive would be what you need.

0
0

Could simplify this by simply binding the Tag property of the control. Quick and dirty, and perhaps not overly elegant, but saves times adding another property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.