32

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.

Community
  • 1
  • 1
Ikky
  • 2,656
  • 12
  • 44
  • 65
  • question similar http://stackoverflow.com/questions/1083159/calling-a-parameterized-constructor-from-xaml – Yang C Nov 28 '16 at 14:24

4 Answers4

65

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!

Nawaz
  • 327,095
  • 105
  • 629
  • 812
  • At last! Someone knows what I wanted...! – Vahid Jan 10 '15 at 09: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](http://stackoverflow.com/a/40901014/2712726) 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. – Patrick Fromberg Dec 06 '16 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>
Community
  • 1
  • 1
Kyle Rosendo
  • 23,930
  • 7
  • 75
  • 114
  • Thanks, i'll take a look into that – Ikky Jan 26 '11 at 07:52
  • 1
    Using property in constructor? When 'New York' value will be set? – Snowbear Jan 26 '11 at 09:53
  • @Snowbear - That's not at all what I said. Was your vote the downvote? – Kyle Rosendo Jan 26 '11 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 '11 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. – Kyle Rosendo Jan 26 '11 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 '11 at 14:37
  • @Snowbear - That's exactly what I have said. If you read the example code, it A. Does not have a parameterized constructor, and B. Passes "New York" via a dependency property. To quote my own answer, I say "This is done with the use of DependencyProperty's, however not via the constructor." – Kyle Rosendo Jan 26 '11 at 16:04
  • @Kyle Rozendo - in your sample you're passing 'City' property to the initControl method from the constructor's body. But the City property will not be initialized yet. – Snowbear Jan 26 '11 at 16:34
  • @Snow - One way or another, I feel that you are nitpicking. The answer itself answers the question . The sample was to illustrate the gist of the use - and you obviously are basing your vote on the piece of "code". I'm not going to fight it though, I'll just remove the line. – Kyle Rosendo Jan 26 '11 at 17:47
1

x:Arguments directive would be what you need.

Yang C
  • 486
  • 5
  • 13
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.

Oyiwai
  • 437
  • 1
  • 3
  • 18