Usercontrol' Constructor with Parameters in C#

UserControl' constructor with parameters in C#

Design decisions made regarding the way Windows Forms works more or less preclude parameterized .ctors for windows forms components. You can use them, but when you do you're stepping outside the generally approved mechanisms. Rather, Windows Forms prefers initialization of values via properties. This is a valid design technique, if not widely used.

This has some benefits, though.

  1. Ease of use for clients. Client code doesn't need to track down a bunch of data, it can immediately create something and just see it with sensible (if uninteresting) results.
  2. Ease of use for the designer. Designer code is clearer and easier to parse in general.
  3. Discourages unusual data dependencies within a single component. (Though even microsoft blew this one with the SplitContainer)

There's a lot of support in forms for working properly with the designer in this technique also. Things like DefaultValueAttribute, DesignerSerializationVisibilityAttribute, and BrowsableAttribute give you the opportunity to provide a rich client experience with minimal effort.

(This isn't the only compromise that was made for client experience in windows forms. Abstract base class components can get hairy too.)

I'd suggest sticking with a parameterless constructor and working within the windows forms design principles. If there are real preconditions that your UserControl must enforce, encapsulate them in another class and then assign an instance of that class to your control via a property. This will give a bit better separation of concern as well.

UserControl Constructor with parameters

Dont use constructors on user controls.

Expose properties with get/set accessors.

Passing constructor parameters to controls created inside InitializeComponent

You need to create a TypeConverter class, and decorate your UserControl with a TypeConverterAttribute(typeof(MyTypeConverter)). The type converter will tell Visual Studio how to create your types - allowing you to control what gets put in the InitializeComponent.
You can go REALLY deep, and actually write a custom CodeDomSerializer, in which you can then write out ANY C# code you want - I used this technique to force the InitializeComponent method to resolve all Forms controls from Castle Windsor! That works really well...

Anyway...

You'll notice MS already uses this technique for types like this:

this.treeView1 = new System.Windows.Forms.TreeView();
this.treeView1.Location = new System.Drawing.Point(72, 104);
this.treeView1.Name = "treeView1";
this.treeView1.Nodes.AddRange(
new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("Node0"),
new System.Windows.Forms.TreeNode("Node1")});

Basically - in your TypeConverter, you override the 'ConverterTo' method, and return a new InstanceDescriptor, which will describe to the WinForms designer, HOW to instantiate your type (what constructor to use, and what arguments to pass).

You can find heaps more information here (including basic implementation):
http://msdn.microsoft.com/en-us/library/ms973818.aspx

InitializeComponent is REALLY powerful, once you get your head around all the extensibility points.
Happy coding!

Passing parameters to UserControl constructor in c# 2.0

I found a solution that uses reflection here

Passing Parameters between xaml window and usercontrol WPF

XAML can't use constructor with parameter. Keep what you've tried with dependency property :

<local:Myusercontrol Param1="user1"/>

then you can try this in constructor :

public Myusercontrol()
{
InitializeComponent();
Loaded += (sender, args) =>
{
UserControlViewModel vm = new UserControlViewModel(Param1);
this.DataContext = vm;
};
}

UserControl Constructor with parameters

Dont use constructors on user controls.

Expose properties with get/set accessors.



Related Topics



Leave a reply



Submit