Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you pass parameters from xaml?

Tags:

c#

wpf

xaml

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.

like image 551
Ikky Avatar asked Jan 26 '11 07:01

Ikky


People also ask

How pass parameters to XAML?

This can be achieved in XAML by using the x:Arguments and x:FactoryMethod attributes: The x:Arguments attribute is used to specify constructor arguments for a non-default constructor, or for a factory method object declaration. For more information, see Passing Constructor Arguments.

What are the ways to pass parameters to and from procedure?

To pass one or more arguments to a procedure In the calling statement, follow the procedure name with parentheses. Inside the parentheses, put an argument list. Include an argument for each required parameter the procedure defines, and separate the arguments with commas.

How do I move data from one page to another in WPF?

The second parameter is the data you want to pass. The target page can handle the NavigationService. LoadCompleted event (or any other appropriate one you prefer), where the object value that was passed to the Navigate() method can be retrieved via the NavigationEventArgs. ExtraData property.


2 Answers

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!

like image 146
Nawaz Avatar answered Sep 28 '22 15:09

Nawaz


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:

  • Dependency Properties Overview
  • DependencyProperty Class MSDN
  • Why Dependency Properties?

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> 
like image 28
Kyle Rosendo Avatar answered Sep 28 '22 15:09

Kyle Rosendo