Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#
Tip/Trick

How to Show Usual Winform as View in XAF

Rate me:
Please Sign up or sign in to vote.
5.00/5 (9 votes)
7 May 2013CPOL2 min read 38.6K   13   10
XAF is a good framework that facilitates software making, but as it provides many benefits, there is the limitation that you have to deal with, one of them is placing fully customized form in a view

Introduction

This tip will show you how to put your usual winforms (designed now) in a "view" which is the main container of XAF UI.

There are official guides as to how to show fully customized forms in XAF, but placing an Action over a view and showing window from there is the core of these guides.

The main disadvantage of this approach is that we cannot call it from the navigation menu and have to call it from ToolBar or RibbonBar. It doesn't make good sense for end users.

Background

I challenge the XAF dev guys and myself to find a generic solution for custom and complex form designing in XAF.

On the one hand, the benefits of XAF are hard to ignore and on the other hand, its limitations and rules are too maddening.

This tip is a partial result of this challenge.

Using the Code

The first step is creating controller that handles navigation pane behaviors. Controllers are classes to change XAF application's flow and implement custom end-user interaction.

This controller will change the default behavior of specific navigation item and show our custom view.

The mentioned controller must inherit from DevExpress.ExpressApp.SystemModule.ShowNavigationItemController.

For rewriting default behavior, we need override ShowNavigationItem method.

C#
 protected override void ShowNavigationItem(SingleChoiceActionExecuteEventArgs e)
       {
// if section, filter navigation node
           if ((e.SelectedChoiceActionItem != null)
          && e.SelectedChoiceActionItem.Enabled.ResultValue
          && e.SelectedChoiceActionItem.Id == "the id of navigation node ")
           {

               Frame workFrame = Application.CreateFrame(TemplateContext.ApplicationWindow);
               View v = new BaseView(typeof(Custom Form ),"Caption","ID of View");
               workFrame.SetView(v);
               e.ShowViewParameters.TargetWindow = TargetWindow.Default;
               e.ShowViewParameters.CreatedView = v;
               return;//Cancel the default processing for this navigation item.

           }
           base.ShowNavigationItem(e);
       }

The above code causes specified navigation item, shows our custom view.

The next step is defining the BaseView class.

C#
class BaseView : DevExpress.ExpressApp.View
   {
       Form frm;
       public BaseView(Type Frm, string Caption, string ID)
           : base(true)
       {
           if (Frm == null)
               throw new Exception("Form is null or invalid");
           if (!Frm.IsSubclassOf(typeof(Form)))
           {
               throw new Exception("Form is null or invalid");
           }
           frm = (Form)Activator.CreateInstance(Frm);

           this.Id = ID;
           this.Caption = Caption;
           this.CurrentObject = null;
       }

       protected override object CreateControlsCore()
       {
           frm.TopLevel = false;
           frm.FormBorderStyle = FormBorderStyle.None;
           frm.Visible = true;
           return frm;
       }

       protected override void LoadModelCore()
       {
          // throw new NotImplementedException();
       }

       protected override void RefreshCore()
       {
          // throw new NotImplementedException();
       }

       protected override void SaveModelCore()
       {
           //throw new NotImplementedException();
       }
   }

In the above code, we inherit from View class (the base of XAF views) and implement our custom rules.

The constructor gets 3 parameters, the first parameter is custom form to show, the second and third are caption and id of generated view.

The magic is in this code.

C#
frm.TopLevel = false;

That allows window nest in a control.

Image 1

Points of Interest

This solution has solved my problem, but further development can be done to create stronger relation to XAF rules, specially in LoadModelCore, RefreshCore, SaveModelCore in BaseView class.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Independent
Iran (Islamic Republic of) Iran (Islamic Republic of)
I start developing software when I was 9 with my commodore 64.

Programming Designer and Quality Supervisor.

Specializing in Web business solutions specially in sale and distribution industry.

Wrote my first “real” program on a commodore 64 which was a shooting game.

A few Highlights:
C#, VB.NET , Win & Web
JavaScript and popular library such jquery
PHP
Delphi
Sql Server

Comments and Discussions

 
Questionthank you! Pin
Ivan Ferrer24-Oct-23 6:52
Ivan Ferrer24-Oct-23 6:52 
Questionwhere is the sample? Pin
RomanRapido9-Jul-15 20:18
RomanRapido9-Jul-15 20:18 
GeneralGreat Work! Pin
belzer2-Oct-13 14:32
belzer2-Oct-13 14:32 
QuestionObject reference not set to an instance of an object Pin
whizkid6-Sep-13 1:00
whizkid6-Sep-13 1:00 
Questionexample Pin
VemotionA21-Aug-13 1:56
VemotionA21-Aug-13 1:56 
AnswerRe: example Pin
Ehsan yazdani rad27-Aug-13 1:56
Ehsan yazdani rad27-Aug-13 1:56 
GeneralRe: example Pin
dauciunas3-Dec-13 0:44
dauciunas3-Dec-13 0:44 
GeneralRe: example Pin
Member 1006519916-Feb-15 1:26
Member 1006519916-Feb-15 1:26 
QuestionIs RightToLeft possible for XAF? Pin
Ehsan Moshiri10-Jun-13 17:45
Ehsan Moshiri10-Jun-13 17:45 
AnswerRe: Is RightToLeft possible for XAF? Pin
Ehsan yazdani rad30-Jun-13 21:09
Ehsan yazdani rad30-Jun-13 21:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.