How do you pass parameters from xaml?

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!

Leave a Comment