Visual inheritance support for complex controls.

WinForms Team Blog
24 May 2006

As all of you probably know, when .NET was released, Microsoft introduced support for visual inheritance - VI. There are a number of articles on the web which praise VI, but this blog entry discusses its limitations when used with complex controls. To do so, we'll start by creating a new control which contains a public collection.

public class MyControl1 : Control {
        Collection items;
        public MyControl1() {
            items = new Collection();
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public Collection Items {
            get { return items; }
        }
}
}

Start VS2005. Create a new windows application, and add the above class to it. After rebulding, you will see MyControl1 at the top of the toolbox. Add MyControl1 to form1, and set MyControl1.Modifiers to public and take a look at the Items collection

Note that we can change it without any limitations. Now create a descendant of form1 successor via visual inheritance(Project-> Add -> NewItem -> Inherited Form). Let’s look at the Items collection in the descendant

 

We can not change collections in the successor, since this is disabled by Microsoft. What is the reason for this? Collections cannot be serialized correctly in VI descendants.

Now lets talk about complex controls. By complex I mean a control which contains a public collection that should serialize correctly at designtime. VI is a powerful thing for rapid UI development but you should take into account the limitation's of collection serialization. Here are a set of recomendations you should follow when creating VI descendants of DX controls. 

  1. You should not change the XtraLayoutControl's layout in a descendant (all changes will be lost!) A workaroud is to split the form’s layout into several parts and create a separate XtraLayoutControl for each one. This approach is shown in the XtraLayoutMainDemo. The topmost part (which contains Save/Load buttons) for all demo modules is inherited.
  2. In VI descendants Moving or Removing inherted columns in the collection can cause problems, you can only safely Add columns to the XtraGridControl. But there are no limitations on changing the properties of existing columns.
  3. You cannot remove inherited items from XtraBars.
  4. VI for inherted collections is not supported by the TreeList, VerticalGrid, PivotGrid, XtraSheduler and XtraCharts.  

The rules above can be summarized by a single general rule: Changing collections in VI descendants leads to problems and you should avoid changing collections in descendants.

Let's now discuss some typical examples of correct and incorrect usage of VI.

XtraBars
Incorrect:
Arrange item links on toolbars in the successor.
Correct: Add all bar items in the base control, place them on toolbars programmatically.
XtraGrid
Incorrect:
Customize XtraGrid in the base form.
Correct: Place an empty XtraGrid on the base form and bind it to data and create columns in successors.
XtraLayout
Incorrect:
Customize layout in the successors.
Correct: Split the form's layout into the several logical square segments and create a separate XtraLayout control for each of them. Customize each layoutControl in the form where is it was created.

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
No Comments

Please login or register to post comments.