windowsforms-15686-controls-and-libraries-application-ui-manager-examples-how-to-create-custom-document-actions.md
Assume that you have a PageGroup containing 2 Documents and a Page that can display any of these Documents separately. The objective is to display different Buttons within these Containers depending on the currently activated Document. Documents are generated based on the User Controls included in the project (see the Documents topic for details). To add a Document Action to this Document, implement the ISupportDocumentActions interface for the related User Control, as demonstrated in the code below.
To implement an interface’s methods, use the corresponding item from the Visual Studio pop-up menu.
Document Actions act like standard Windows Forms buttons. When creating an Action, you need to specify the execute method that implements the Action’s functionality, and (optionally) the canExecute method, which specifies the set of criteria and checks whether or not the current Document meets these criteria. You can specify these methods as shown in the code below.
You now have two Document Actions that act like simple “push” buttons in your first Document. Actions can also act like check buttons that have two states - checked and unchecked. To create these Actions in your second Document, implement the ISupportDocumentActions interface (as you did before) and add two DocumentCheckAction objects to the IDocumentActionsArgs.DocumentActions collection.
To display the Actions you have added, hide the default PageGroup header. To do so, set the IPageGroupDefaultProperties.ShowPageHeaders property to DefaultBoolean.False.
Finally, the code below illustrates how to add the ‘Hello’ Document Action. This is an example of an Action that does not have a canExecute method, and thus is displayed within each Content Container.
The animation below shows the result. The Page container has different Actions depending on the activated Document (DocumentActions for the first Document and DocumentCheckActions for the second Document). The PageGroup container also changes its Actions when navigating through its items, and displays either ‘Document 1’ or ‘Document 2’ navigation Actions. The ‘Hello’ action is displayed within any Content Container regardless of the currently activated Document.
This example demonstrates how to add How To: Create Custom Document Actions to your WindowsUIView application.
using System.Windows.Forms;
using DevExpress.XtraBars.Docking2010.Views.WindowsUI;
using WindowsFormsApplication1.ViewModels;
namespace WindowsFormsApplication1.Views {
public partial class ucModuleA : UserControl, ISupportDocumentActions {
ViewModelModuleA viewModel;
public ucModuleA(ViewModelModuleA viewModel) {
this.viewModel = viewModel;
InitializeComponent();
}
#region ISupportDocumentActions Members
public void OnQueryDocumentActions(IDocumentActionsArgs args) {
args.DocumentActions.Add(new DocumentAction(DoAction1) { Caption = "Action 1" });
args.DocumentActions.Add(new DocumentAction(CanDoAction2, DoAction2) { Caption = "Action 2" });
}
#endregion
void DoAction1(Document document) {
bool prev = viewModel.AllowAnotherAction;
viewModel.DoSomeAction();
MessageBox.Show("Some Action");
if(!prev && prev != viewModel.AllowAnotherAction)
MessageBox.Show("Action 2 enabled");
}
bool CanDoAction2(Document document) {
return viewModel.AllowAnotherAction;
}
void DoAction2(Document document) {
MessageBox.Show("Action 2 disabled");
viewModel.ResetSomeAction();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1.ViewModels {
public class ViewModelModuleB {
public bool Flag { get; set; }
public bool IsSpecificState { get; set; }
public void SetSpecicState() {
IsSpecificState = true;
}
public void ResetSpecificState() {
IsSpecificState = false;
}
}
}
using System.Windows.Forms;
using DevExpress.XtraBars.Docking2010.Views.WindowsUI;
using DevExpress.XtraBars.Docking2010.Views.WindowsUI;
using WindowsFormsApplication1.ViewModels;
namespace WindowsFormsApplication1.Views {
public partial class ucModuleB : UserControl, ISupportDocumentActions {
ViewModelModuleB viewModel;
public ucModuleB(ViewModelModuleB viewModel) {
this.viewModel = viewModel;
InitializeComponent();
}
#region ISupportDocumentActions Members
public void OnQueryDocumentActions(IDocumentActionsArgs args) {
args.DocumentActions.Add(new DocumentCheckAction(() => viewModel.Flag, OnToggle) { Caption = "Flag" });
args.DocumentActions.Add(new DocumentCheckAction(() => viewModel.IsSpecificState, OnCheck, OnUncheck) { Caption = "Specific State" });
}
#endregion
void OnToggle(Document document) {
viewModel.Flag = !viewModel.Flag;
MessageBox.Show("Flag is " + (viewModel.Flag).ToString());
}
void OnCheck(Document document) {
viewModel.SetSpecicState();
MessageBox.Show("Specific state enabled");
}
void OnUncheck(Document document) {
viewModel.ResetSpecificState();
MessageBox.Show("Specific state disabled");
}
}
}
using System.Windows.Forms;
using DevExpress.XtraBars.Docking2010.Views;
using DevExpress.XtraBars.Docking2010.Views.WindowsUI;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
pageGroup1.Properties.ShowPageHeaders = DevExpress.Utils.DefaultBoolean.False;
windowsUIView1.QueryDocumentActions += windowsUIView1_QueryDocumentActions;
windowsUIView1.TileClick += windowsUIView1_TileClick;
windowsUIView1.AllowCaptionDragMove = DevExpress.Utils.DefaultBoolean.True;
}
void windowsUIView1_TileClick(object sender, TileClickEventArgs e) {
if(e.Tile == document1Tile)
page1.Document = document1;
if(e.Tile == document2Tile)
page1.Document = document2;
}
void windowsUIView1_QueryControl(object sender, QueryControlEventArgs e) {
if(e.Document == document1)
e.Control = new Views.ucModuleA(new ViewModels.ViewModelModuleA());
if(e.Document == document2)
e.Control = new Views.ucModuleB(new ViewModels.ViewModelModuleB());
}
//
void windowsUIView1_QueryDocumentActions(object sender, QueryDocumentActionsEventArgs e) {
e.DocumentActions.Add(new DocumentAction(Hello) { Caption = "Hello" });
if (e.Source == pageGroup1) {
e.DocumentActions.Add(new DocumentAction((doc) => doc == document2,
(doc) => windowsUIView1.ActivateDocument(document1)) { Caption = "Document 1" });
e.DocumentActions.Add(new DocumentAction((doc) => doc == document1,
(doc) => windowsUIView1.ActivateDocument(document2)) { Caption = "Document 2" });
}
}
void Hello(Document document) {
MessageBox.Show("Hello from " + document.Caption);
}
}
}
namespace WindowsFormsApplication1.ViewModels {
public class ViewModelModuleA {
public bool AllowAnotherAction { get; private set; }
internal void DoSomeAction() {
AllowAnotherAction = true;
}
internal void ResetSomeAction() {
AllowAnotherAction = false;
}
}
}
Imports Microsoft.VisualBasic
Imports System.Windows.Forms
Imports DevExpress.XtraBars.Docking2010.Views
Imports DevExpress.XtraBars.Docking2010.Views.WindowsUI
Namespace WindowsFormsApplication1
Public Partial Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
pageGroup1.Properties.ShowPageHeaders = DevExpress.Utils.DefaultBoolean.False
AddHandler windowsUIView1.QueryDocumentActions, AddressOf windowsUIView1_QueryDocumentActions
AddHandler windowsUIView1.TileClick, AddressOf windowsUIView1_TileClick
End Sub
Private Sub windowsUIView1_TileClick(ByVal sender As Object, ByVal e As TileClickEventArgs)
If e.Tile Is document1Tile Then
page1.Document = document1
End If
If e.Tile Is document2Tile Then
page1.Document = document2
End If
End Sub
Private Sub windowsUIView1_QueryControl(ByVal sender As Object, ByVal e As QueryControlEventArgs) Handles windowsUIView1.QueryControl
If e.Document Is document1 Then
e.Control = New Views.ucModuleA(New ViewModels.ViewModelModuleA())
End If
If e.Document Is document2 Then
e.Control = New Views.ucModuleB(New ViewModels.ViewModelModuleB())
End If
End Sub
'
Private Sub windowsUIView1_QueryDocumentActions(sender As Object, e As QueryDocumentActionsEventArgs)
e.DocumentActions.Add(New DocumentAction(Sub(doc) Hello(e.Document)) With { _
.Caption = "Hello" _
})
If e.Source Is pageGroup1 Then
e.DocumentActions.Add(New DocumentAction(Function(doc) doc Is document2, Sub(doc) windowsUIView1.ActivateDocument(document1)) With { _
.Caption = "Document 1" _
})
e.DocumentActions.Add(New DocumentAction(Function(doc) doc Is document1, Sub(doc) windowsUIView1.ActivateDocument(document2)) With { _
.Caption = "Document 2" _
})
End If
End Sub
Private Sub Hello(ByVal document As Document)
MessageBox.Show("Hello from " & document.Caption)
End Sub
End Class
End Namespace
Imports Microsoft.VisualBasic
Imports System.Windows.Forms
Imports DevExpress.XtraBars.Docking2010.Views.WindowsUI
Imports WindowsFormsApplication1.ViewModels
Namespace WindowsFormsApplication1.Views
Public Partial Class ucModuleB
Inherits UserControl
Implements ISupportDocumentActions
Private viewModel As ViewModelModuleB
Public Sub New(ByVal viewModel As ViewModelModuleB)
Me.viewModel = viewModel
InitializeComponent()
End Sub
#Region "ISupportDocumentActions Members"
Public Sub OnQueryDocumentActions(args As IDocumentActionsArgs) Implements ISupportDocumentActions.OnQueryDocumentActions
args.DocumentActions.Add(New DocumentCheckAction(Function() viewModel.Flag, Sub() OnToggle(args.Document)) With { _
.Caption = "Flag" _
})
args.DocumentActions.Add(New DocumentCheckAction(Function() viewModel.IsSpecificState, Sub() OnCheck(args.Document), Sub() OnUncheck(args.Document)) With { _
.Caption = "Specific State" _
})
End Sub
#End Region
Private Sub OnToggle(ByVal document As Document)
viewModel.Flag = Not viewModel.Flag
MessageBox.Show("Flag is " & (viewModel.Flag).ToString())
End Sub
Private Sub OnCheck(ByVal document As Document)
viewModel.SetSpecicState()
MessageBox.Show("Specific state enabled")
End Sub
Private Sub OnUncheck(ByVal document As Document)
viewModel.ResetSpecificState()
MessageBox.Show("Specific state disabled")
End Sub
End Class
End Namespace
Imports Microsoft.VisualBasic
Imports System.Windows.Forms
Imports DevExpress.XtraBars.Docking2010.Views.WindowsUI
Imports WindowsFormsApplication1.ViewModels
Namespace WindowsFormsApplication1.Views
Public Partial Class ucModuleA
Inherits UserControl
Implements ISupportDocumentActions
Private viewModel As ViewModelModuleA
Public Sub New(ByVal viewModel As ViewModelModuleA)
Me.viewModel = viewModel
InitializeComponent()
End Sub
#Region "ISupportDocumentActions Members"
Public Sub OnQueryDocumentActions(args As IDocumentActionsArgs) Implements ISupportDocumentActions.OnQueryDocumentActions
args.DocumentActions.Add(New DocumentAction(Sub(doc) DoAction1(args.Document)) With { _
.Caption = "Action 1" _
})
args.DocumentActions.Add(New DocumentAction(Function(doc) CanDoAction2(args.Document), Sub(doc) DoAction2(args.Document)) With { _
.Caption = "Action 2" _
})
End Sub
#End Region
Private Sub DoAction1(ByVal document As Document)
Dim prev As Boolean = viewModel.AllowAnotherAction
viewModel.DoSomeAction()
MessageBox.Show("Some Action")
If (Not prev) AndAlso prev <> viewModel.AllowAnotherAction Then
MessageBox.Show("Action 2 enabled")
End If
End Sub
Private Function CanDoAction2(ByVal document As Document) As Boolean
Return viewModel.AllowAnotherAction
End Function
Private Sub DoAction2(ByVal document As Document)
MessageBox.Show("Action 2 disabled")
viewModel.ResetSomeAction()
End Sub
End Class
End Namespace
Imports Microsoft.VisualBasic
Imports System
Namespace WindowsFormsApplication1.ViewModels
Public Class ViewModelModuleA
Public Property AllowAnotherAction() As Boolean
Get
Return m_AllowAnotherAction
End Get
Private Set(value As Boolean)
m_AllowAnotherAction = Value
End Set
End Property
Private m_AllowAnotherAction As Boolean
Friend Sub DoSomeAction()
AllowAnotherAction = True
End Sub
Friend Sub ResetSomeAction()
AllowAnotherAction = False
End Sub
End Class
End Namespace
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Namespace WindowsFormsApplication1.ViewModels
Public Class ViewModelModuleB
Public Property Flag() As Boolean
Get
Return m_Flag
End Get
Set(value As Boolean)
m_Flag = Value
End Set
End Property
Private m_Flag As Boolean
Public Property IsSpecificState() As Boolean
Get
Return m_IsSpecificState
End Get
Set(value As Boolean)
m_IsSpecificState = Value
End Set
End Property
Private m_IsSpecificState As Boolean
Public Sub SetSpecicState()
IsSpecificState = True
End Sub
Public Sub ResetSpecificState()
IsSpecificState = False
End Sub
End Class
End Namespace