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.

9 comment(s)
Anonymous
Renaud Bompuis
Additionally, have a look at this knowledge base article too to setup visual inheritance in VS2005 for devexpress components:
http://devexpress.com/kb=A2912
7 June, 2006
Roman Kalachik (DevExpress)
Roman Kalachik (DevExpress)
You can enable VI using that hack. But you will have problems if you ignore recommendations above
8 June, 2006
Alex Danvy
Alex Danvy

Using more than one XtraLayoutControl on a single form is really not end-user friendly : It will be hard to explain the restrcitions. For example, why some items can't be moved to some places (other layouts).

I would be very happy to see DevExpress working on the inheritance restriction. There might have workaround like an AdditionalLayoutItems component to drop on descendant that will be merged by the LayoutControl, etc.

18 September, 2007
Puc Software
Puc Software

Sorry to see nothing has changed now that I've moved to VS.NET 2008. Eventually, I'll just give up on Visual inheritance and modular design altogether. Very frustrating.

14 May, 2008
Roman Kalachik (DevExpress)
Roman Kalachik (DevExpress)

Probably the article is not clear enough: Visual Studio has some limitations in serializing collections in VI descendants. This limitation cannot be fixed at the third party component vendors side. It should be fixed by the Microsoft.

21 May, 2008
Michael Proctor [DX-Squad]
Michael Proctor [DX-Squad]

Hey Roman,

Realise this was done AGES ago, but any word from MS if they have improved the VI in 2010?

Haven't bothered looking at the 2010 beta2, would be keen to know if it has changed.

Cheers

25 October, 2009
Roman Kalachik (DevExpress)
Roman Kalachik (DevExpress)

Michael, please review this issue:

connect.microsoft.com/.../ViewFeedback.aspx

26 October, 2009
Roman Kalachik (DevExpress)
Roman Kalachik (DevExpress)
26 October, 2009
Guido Geurts
Guido Geurts

very sad this is still not fixed by microsoft in 2018...

21 November, 2018

Please login or register to post comments.