in
Forums
Blogs
Files
Devexpress.Com
ClientCenter
Support Center
DevExpress Channel

Windows Forms

Visual inheritance support for complex controls.

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. There are no known limitations for XtraEditors when using VI
  2. 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.
  3. 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.
  4. You cannot remove inherited items from XtraBars.
  5. 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.

Published May 24 2006, 07:47 AM by Roman K (Developer Express)

Comments

 

Renaud Bompuis said:

Additionally, have a look at this knowledge base article too to setup visual inheritance in VS2005 for devexpress components:
http://devexpress.com/kb=A2912
June 7, 2006 11:35 AM
 

Roman K (Developer Express) said:

You can enable VI using that hack. But you will have problems if you ignore recommendations above
June 8, 2006 2:38 AM
 

Alex Danvy [DX-Squad] said:

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.

September 18, 2007 5:50 AM
 

Puc Software said:

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.

May 14, 2008 9:52 AM
 

Roman K (Developer Express) said:

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.

May 21, 2008 7:38 AM

Leave a Comment

(required)  
(optional)
(required)  
Verification code: Required
   
Add
Copyright © 1998-2008 Developer Express Inc.
ALL RIGHTS RESERVED