Smart tag – Single Binding


I’d like to continue describing how to create a smart tag. Previously I wrote what is smart tag and how to create it. In this post I’d like to tell how to create binding for the single field.

I assume that you have already created a custom user control, apply designer attribute and ready for to go ahead. As in the previous post I strongly advise to use ReSharper.

I’m going to show you how to create a similar functionality for your control’s smart tag as at Property view

Implementing this you can save a lot of time that was spent on browsing Property view in order to setup appearance and behavior.

Is there standard way?

I didn’t find any “easy” and short standard way how to make it. Sad, but true. Using Google I found only four questions without answers or answer was related to the Web development. I was frustrated for some time, but don’t give up! If somebody creates something, another man can repeat it. Followed by this logic statement I’ve remember that Microsoft publish source codes for .Net classes and few minutes later I found where to get it and start the investigation.

In order to my knowledge of control’s development, I was looking for the ComboBox declaration and what designer was used for binding field. Unfortunately there wasn’t a common designer, but it’s not a problem.   The following code was placed:

Editor("System.Windows.Forms.Design.DesignBindingEditor, System.Design", typeof (UITypeEditor))]

You can check out the search results for DesignBindingEditor in msdn by searching in knowledge base or by code base. Result is EMPTY!

Okay, editor and type convertor must be standard anyway and somehow should be founded. But it’s not the only thing that you should do. It will be too much easy! ))


It’s our way to understand how things works (without documentation). Copy-paste and slightly change parameters to see how it affects on the result.

As you know in designer class properties are for displayed fields and all code placed in “get”/“set”. There a lot of stuff, scared a little bit. =)

public object BoundSelectedValue {
    get {
        System.Windows.Forms.Binding b = GetSelectedValueBinding();
        string dataMember;
        object dataSource;
        if (b == null) {
            dataMember = null;
            dataSource = null;
        } else {
            dataMember = b.BindingMemberInfo.BindingMember;
            dataSource = b.DataSource;
        string typeName = string.Format(System.Globalization.CultureInfo.InvariantCulture,  "System.Windows.Forms.Design.DesignBinding, {0}", typeof(ControlDesigner).Assembly.FullName);
        _boundSelectedValue = TypeDescriptor.CreateInstance(null, Type.GetType(typeName), new Type[] { typeof(object), typeof(string) }, new object[] { dataSource, dataMember });
        return _boundSelectedValue;
set {
    if (value is String) {
        PropertyDescriptor pd = TypeDescriptor.GetProperties(this)["BoundSelectedValue"];
        TypeConverter tc = pd.Converter;
        _boundSelectedValue = tc.ConvertFrom(new EditorServiceContext(_owner), System.Globalization.CultureInfo.InvariantCulture, value);
    } else {
       _boundSelectedValue = value;
       if (value != null) {
           object dataSource = TypeDescriptor.GetProperties(_boundSelectedValue)["DataSource"].GetValue(_boundSelectedValue);
           string dataMember = (string)TypeDescriptor.GetProperties(_boundSelectedValue)["DataMember"].GetValue(_boundSelectedValue);
SetSelectedValueBinding(dataSource, dataMember);


Let’s take a close looking at “get”. At the line 3 getting all controls from components and searching binding with necessary name – for ComboBox it is SelectedValue. If nothing found it means that binding wasn’t set evere before. Otherwise dataMember and dataSource gets for creating string representation for user by DesighnBindingConverter.

During experiments “get” was the easy part. More problem appears with set. If you havealready set binding on some field, it is presents as a string value and have to be parsed. For this action is the first part of “if” statement. And here, I suppose, we get the answer why this binding method wasn’t publishied. In the row 21 creating EditorServiceContext that is internal class in .Net sources. I have had to find it and recreate it for my needs. I just copy-paste entire class without any changes into my solution. The last statement with SetSelectedValueBinding was also copied without changes.


I did refactoring for all this code to get more short and easy to use code. For example like this

Editor("System.Windows.Forms.Design.DesignBindingEditor, System.Design", typeof (UITypeEditor))]
public object DefBindProperty {
    get { return GetBind("DefBind"); }
    set { SetBind("DefBind", value, "DefBindProperty"); }

GetBind and SetBind declared in the class ControlActionListBase. In this case our ActionList class should be inherited from ControlActionListBase, and ControlActionListBase from DesignerActionList.

public class MyControlActionList : ControlActionListBase { … }

public class ControlActionListBase : DesignerActionList { … }

Making this you can leave in custom action lists only necessary properties, methods and overridden GetSortedActionItems method. All infrastructure code will be in base class. Wrote and forgot. =) When you bind something with this code you’ll find in final control designer auto generated code like this:

this.myControl1.DataBindings.Add(new System.Windows.Forms.Binding("DefBind", this.bindingSource, "StringField", true));

You can find final result in source code.

For now we can create new and extend exists controls with binding to single properties. I hope it will save to you a lot of time during developing.

Tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>