DockLayoutManager.ItemTemplateSelector Property

Gets or sets a DataTemplateSelector object that provides a way to choose a DataTemplate to render items stored in the DockLayoutManager.ItemsSource collection.

Namespace: DevExpress.Xpf.Docking

Assembly: DevExpress.Xpf.Docking.v20.1.dll

Declaration

public DataTemplateSelector ItemTemplateSelector { get; set; }
Public Property ItemTemplateSelector As DataTemplateSelector

Property Value

Type Description
DataTemplateSelector

A DataTemplateSelector object that provides a way to choose a DataTemplate to render items stored in the DockLayoutManager.ItemsSource collection.

Remarks

A DataTemplate allows you to specify the visualization for DockLayoutManager items stored in the DockLayoutManager.ItemsSource collection. Using a DataTemplateSelector you can apply your own logic to choose a DataTemplate. To implement this functionality, define a class derived from a DataTemplateSelector, override the SelectTemplate method and assign an instance of this class to the ItemTemplateSelector property.

If no DataTemplateSelector object is assigned to the ItemTemplateSelector property, a DataTemplate assigned to the DockLayoutManager.ItemTemplate property will be used. If the DataTemplateSelector returns null no DataTemplate will be used.

To learn more, see Choosing Templates Based on Custom Logic.

Examples

The following example shows how to create layout items (LayoutPanel and DocumentPanel objects) from a data source according to the MVVM design pattern and how to implement template selection.

In this example, the MainViewModel class provides the Items collection that contains objects to be rendered as layout items in the DockLayoutManager. An instance of the MainViewModel class is assigned to the Window's DataContext. The DockLayoutManager.ItemsSource property is bound to the Items property in the MainViewModel. This way, the DockLayoutManager gets the source of objects to be visualized as layout items.

The Items collection contains items of two types:

  • ViewModel objects - contain data for creating LayoutPanels
  • DocumentViewModel objects - contain data for creating DocumentPanels

There are two templates declared in XAML that render input objects as either LayoutPanels or DocumentPanels.

The custom DockItemTemplateSelector class overrides the SelectTemplate method to choose a template that corresponds to a specific item in the DockLayoutManager.ItemsSource. This template selector is assigned in XAML to the DockLayoutManager.ItemTemplateSelector property.

In this example, the DockLayoutManager contains two groups explicitly created in XAML: a LayoutGroup named panelHost and a DocumentGroup named documentHost. Items from the Items collection are automatically placed in one of these groups based on their IMVVMDockingProperties.TargetName property values.

Imports DevExpress.Xpf.Docking
Imports DevExpress.Mvvm
Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes

Namespace WpfApplication19
    ''' <summary>
    ''' Interaction logic for MainWindow.xaml
    ''' </summary>
    Partial Public Class MainWindow
        Inherits Window

        Public Sub New()
            InitializeComponent()
        End Sub
    End Class
    Public Class MainViewModel
        Inherits ViewModelBase

        Public Sub New()
            Items = New ObservableCollection(Of Object)() From { _
                New ViewModel() With {.DisplayName = "Item1"}, _
                New ViewModel() With {.DisplayName = "Item2"}, _
                New DocumentViewModel() With {.DisplayName = "Item3"}, _
                New DocumentViewModel() With {.DisplayName = "Item4"} _
            }
        End Sub
        ' Fields...
        Private _Items As ObservableCollection(Of Object)

        Public Property Items() As ObservableCollection(Of Object)
            Get
                Return _Items
            End Get
            Private Set(ByVal value As ObservableCollection(Of Object))
                SetProperty(_Items, value, "Items")
            End Set
        End Property
    End Class
    Public Class DocumentViewModel
        Inherits ViewModel
        Implements IMVVMDockingProperties

        Protected Overrides Function GetTargetName() As String
            Return "documentHost"
        End Function
    End Class
    Public Class ViewModel
        Inherits ViewModelBase
        Implements IMVVMDockingProperties

        ' Fields...
        Private _DisplayName As String

        Public Property DisplayName() As String
            Get
                Return _DisplayName
            End Get
            Set(ByVal value As String)
                SetProperty(_DisplayName, value, "DisplayName")
            End Set
        End Property
        Protected Overridable Function GetTargetName() As String
            Return "panelHost"
        End Function
        #Region "IMVVMDockingProperties Members"

        Private Property IMVVMDockingProperties_TargetName() As String Implements IMVVMDockingProperties.TargetName
            Get
                Return GetTargetName()
            End Get
            Set(ByVal value As String)
                Throw New NotImplementedException()
            End Set
        End Property

        #End Region
    End Class
    Public Class DockItemTemplateSelector
        Inherits DataTemplateSelector

        Public Property LayoutPanelTemplate() As DataTemplate
        Public Property DocumentPanelTemplate() As DataTemplate
        Public Overrides Function SelectTemplate(ByVal item As Object, ByVal container As DependencyObject) As DataTemplate
            Return If(TypeOf item Is DocumentViewModel, DocumentPanelTemplate, LayoutPanelTemplate)
        End Function
    End Class
End Namespace
See Also