User Data

There are at least three ways to add custom properties (or user data) to controls in Wisej.NET:

  1. Extend the class

  2. Tag property

  3. UserData property

Extending the class is the most flexible approach but in some cases it's not feasible to create a new class of a control just to add a value use in a small specific context.

The Tag property is available at design time at provides a quick way to add custom information to an instance of a control.

The UserData property is a dynamic object that allows you to add any kind of fields to a control without having to create a new class.

Extend the class

All Wisej.NET control classes can be used as the base class of your classes. You can create new types of buttons, tab pages, tree nodes, grid cells, grid rows, columns, forms, etc.

For example, if you need to have a TreeNode instance that is associated with a Customer instance you can create a CustomerTreeNode class.

class CustomerTreeNode : Wisej.Web.TreeNode
{
    public Customer Customer {get; set; }
}

...

this.treeView1.Nodes.Add(new CustomerTreeNode
{
    Customer = goodCustomer
});

You can use the new CustomerTreeNode class just like the base TreeNode class, including at design time. When adding nodes at design time, the designer will show a drop down button to pick the specific TreeNode class to add.

Tag Property

All controls and components in Wisej.NET expose the Tag property of type object. You can "tag" any control or component with any value of any type.

UserData property

The UserData property is similar to the Tag property except that it's a dynamic object that can hold any number of fields of any kind.

For example, we can "attach" a Customer instance to a TreeNode without creating a CustomerTreeNode class like this:

var node = new TreeNode();
node.UserData.Customer = goodCustomer;
this.treeView1.Nodes.Add(node);

// Alternative using the property name.
var node = new TreeNode();
node.UserData["Customer"] = goodCustomer;
this.treeView1.Nodes.Add(node);

As shown in the code snippet above, the UserData property is a dynamic object and a dictionary.

To check whether an object has any data in the UserData dictionary without forcing the creation of the storage object, use the HasUserData property.

Last updated