3

I want to assign a value right when initializing a new UserControl:

public partial class MyUserControl : UserControl
{
    public MyUserControl(int id)
    {
        InitializeComponent();

        //.. do something with id
    }

    // ...
}

Is it possible to pass a value to constructor (id in my case) from xaml?

<CustomControls:MyUserControl />

(Yes I can define a dependency property or make the control in code behind, but that doesn't help)

user3293835
  • 789
  • 1
  • 13
  • 27
  • [This is for WPF](http://stackoverflow.com/questions/1083159/calling-a-parametrized-constructor-from-xaml) but I believe wp8 will also have it. – Sriram Sakthivel Mar 31 '14 at 03:44
  • didn't have, I think. – user3293835 Mar 31 '14 at 03:57
  • no it isn't possible as far as I can see. Even the XAML 2009 feature to pass constructor parameter as in link from @SriramSakthivel, it isn't *practically* [usable in WPF](http://stackoverflow.com/questions/14347181/how-to-fix-xaml2009-language-construct-is-not-allowed-here), moreover in Windows Phone. – har07 Mar 31 '14 at 07:08
  • @har07 Thanks, I wasn't aware of that, just came over that link and shared. So that means where those xaml2009 features are useful? only from `XamlPad` ? – Sriram Sakthivel Mar 31 '14 at 07:19
  • See "If you want to pass data to your control, better choice would be DependencyProperty - example here." link in [Romasz answer](https://stackoverflow.com/a/22756222/199364) – ToolmakerSteve Jun 24 '18 at 03:09

2 Answers2

0

Yeah, that's possible. You can create a user control programmatically. Then, you can use any constructor you want. Here's a sample:

Supposing, that we have a usercontrol, that assigns a value to textbox on initialization:

  public ControlWithP(int i)
        {
            InitializeComponent();
            tb.Text = i.ToString();
        }

Add this control to page:

 public SamplePage()
        {
            InitializeComponent();
            ControlWithP cwp = new ControlWithP(1);
            this.sp.Children.Add(cwp);
        }

where sp is StackPanel control. Same thing with adding user control to Grid.

see the result.

Is this, what you wanted?

Olter
  • 1,129
  • 1
  • 19
  • 38
0

From XAML-2009 you could do this with x:Arguments Directive but Windows Phone is using 2006 (for now) so it is not possible.

So to use your control from XAML you need a default contructor (parameterless).

I think you could use a little workaround, by using specially designed property for this:

public partial class MyControl : UserControl
{
    private string myValue = "Default";
    public string MyValue
    {
        get { return myValue; }
        set
        {
            myValue = value;
            // alternatively you can add some code here which 
            // will be invoked after control is created
        }
    }

    public MyControl()
    {
        InitializeComponent();
    }
}

then in XAML:

<local:MyControl MyValue="From xaml"/>

Just after the control is created, the property is set and its code invoked - so it can alternatively be used as additional part of code run during creation.

If you want to pass data to your control, better choice would be DependencyProperty - example here.

Community
  • 1
  • 1
Romasz
  • 29,062
  • 12
  • 73
  • 140