How to: Load an External Window or UserControl into a DocumentPanel

  • 3 minutes to read

You can define a Window, Page or UserControl in external XAML files and then, with DXDocking, load their contents into DocumentPanel objects. This example demonstrates how to load an external Window and UserControl into DocumentPanels. In the example three approaches are demonstrated:

  1. The contents of MyWindow.xaml is loaded into a DocumentPanel at design time (in XAML) via the DocumentPanel.Content property. The Content property accepts a Uri object, which must refer to a XAML file defining a Window, Page or UserControl.

    <dxdo:DocumentPanel 
      x:Name="docPanel2" 
      Caption="Panel 2" 
      Content="{dxdo:RelativeUri UriString=CustomWindows\\MyWindow.xaml}"/>
    
  2. The DocumentPanel.Content property is set to a Uri object at runtime:

    docPanel1.Content = new Uri(\@"CustomWindows\MyWindow1.xaml", UriKind.Relative);
    
  3. The DockController.AddDocumentPanel method creates a new DocumentPanel object and loads the contents of an external XAML file into the created panel.

panel1 = dockLayoutManager1.DockController.AddDocumentPanel(
    documentGroup1,
    new Uri(\@"CustomWindows\UserControl1.xaml", 
    UriKind.Relative)
);
panel1.Caption = "Document " + (ctr++).ToString();

In the example, the XAML file defines a UserControl object. The loaded UserControl is accessed via the DocumentPanel's Control property and then a method on the UserControl is invoked.

//...

(panel1.Control as UserControl1).SetDataContext(imageInfo);

You can see this in action by clicking the "Set DataContext for UserControl" button in the example.

Imports Microsoft.VisualBasic
Imports System
Imports System.Windows
Imports System.Windows.Media.Imaging
Imports DevExpress.Xpf.Docking

Namespace DocumentPanel_Content
    Partial Public Class MainWindow
        Inherits Window
        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub ActivateItem(ByVal item As BaseLayoutItem)
            dockLayoutManager1.LayoutController.Activate(item)
        End Sub

        'Load MyWindow1.xaml into docPanel1
        Private Sub btnSetContent_ItemClick(ByVal sender As Object, ByVal e As DevExpress.Xpf.Bars.ItemClickEventArgs)
            ActivateItem(docPanel1)
            docPanel1.Content = New Uri("CustomWindows\MyWindow1.xaml", UriKind.Relative)
        End Sub

        Private panel1 As DocumentPanel = Nothing
        Private ctr As Integer = 1

        Private Sub btnAddDockPanel_ItemClick(ByVal sender As Object, ByVal e As DevExpress.Xpf.Bars.ItemClickEventArgs)
            panel1 = dockLayoutManager1.DockController.AddDocumentPanel(documentGroup1, New Uri("CustomWindows\UserControl1.xaml", UriKind.Relative))
            panel1.Caption = "Document " & (ctr).ToString()
            ctr += 1

            DocumentPanel.SetMDISize(panel1, New Size(400, 300))
            DocumentPanel.SetMDILocation(panel1, New Point(200, 200))
            ActivateItem(panel1)
        End Sub
        Private Sub btnSetDataContext_ItemClick(ByVal sender As Object, ByVal e As DevExpress.Xpf.Bars.ItemClickEventArgs)
            If panel1 Is Nothing Then
                Return
            End If
            Dim uri As New Uri("/Images/IMG_1391.jpg", UriKind.Relative)
            Dim imageInfo As New ImageInfo()
            imageInfo.Source = New BitmapImage(uri)
            imageInfo.Description = uri.ToString()
            'Invoke a method on the loaded UserControl
            TryCast(panel1.Control, UserControl1).SetDataContext(imageInfo)

            ActivateItem(panel1)
        End Sub
    End Class
End Namespace