wpf-devexpress-dot-xpf-dot-docking-dot-docklayoutmanager-b649b464.md
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.v25.2.dll
NuGet Package : DevExpress.Wpf.Docking
public DataTemplateSelector ItemTemplateSelector { get; set; }
Public Property ItemTemplateSelector As DataTemplateSelector
| Type | Description |
|---|---|
| DataTemplateSelector |
A DataTemplateSelector object that provides a way to choose a DataTemplate to render items stored in the DockLayoutManager.ItemsSource collection.
|
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.
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:
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
<Window x:Class="WpfApplication19.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxdo="http://schemas.devexpress.com/winfx/2008/xaml/docking"
xmlns:local="clr-namespace:WpfApplication19"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:MainViewModel />
</Window.DataContext>
<Grid>
<dxdo:DockLayoutManager ItemsSource="{Binding Items}">
<dxdo:DockLayoutManager.Resources>
<DataTemplate x:Key="layoutPanelTemplate">
<dxdo:LayoutPanel Caption="{Binding DisplayName}">
<dxdo:LayoutPanel.ContentTemplate>
<DataTemplate>
<Border Background="AliceBlue">
<ContentPresenter Content="{Binding DisplayName}" />
</Border>
</DataTemplate>
</dxdo:LayoutPanel.ContentTemplate>
</dxdo:LayoutPanel>
</DataTemplate>
<DataTemplate x:Key="documentPanelTemplate">
<dxdo:DocumentPanel Caption="{Binding DisplayName}">
<dxdo:LayoutPanel.ContentTemplate>
<DataTemplate>
<Border Background="AntiqueWhite">
<ContentPresenter Content="{Binding DisplayName}" />
</Border>
</DataTemplate>
</dxdo:LayoutPanel.ContentTemplate>
</dxdo:DocumentPanel>
</DataTemplate>
</dxdo:DockLayoutManager.Resources>
<dxdo:DockLayoutManager.ItemTemplateSelector>
<local:DockItemTemplateSelector LayoutPanelTemplate="{StaticResource ResourceKey=layoutPanelTemplate}" DocumentPanelTemplate="{StaticResource ResourceKey=documentPanelTemplate}" />
</dxdo:DockLayoutManager.ItemTemplateSelector>
<dxdo:LayoutGroup>
<dxdo:LayoutGroup x:Name="panelHost" />
<dxdo:DocumentGroup x:Name="documentHost" />
</dxdo:LayoutGroup>
</dxdo:DockLayoutManager>
</Grid>
</Window>
<Application x:Class="WpfApplication19.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
Imports System
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data
Imports System.Linq
Imports System.Windows
Namespace WpfApplication19
''' <summary>
''' Interaction logic for App.xaml
''' </summary>
Partial Public Class App
Inherits Application
End Class
End Namespace
using DevExpress.Xpf.Docking;
using DevExpress.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication19 {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
}
public class MainViewModel : ViewModelBase {
public MainViewModel() {
Items = new ObservableCollection<object>() { new ViewModel() { DisplayName = "Item1" }, new ViewModel() { DisplayName = "Item2" }, new DocumentViewModel() { DisplayName = "Item3" }, new DocumentViewModel() { DisplayName = "Item4" } };
}
// Fields...
private ObservableCollection<object> _Items;
public ObservableCollection<object> Items {
get { return _Items; }
private set { SetProperty(ref _Items, value, "Items"); }
}
}
public class DocumentViewModel : ViewModel, IMVVMDockingProperties {
protected override string GetTargetName() {
return "documentHost";
}
}
public class ViewModel : ViewModelBase, IMVVMDockingProperties {
// Fields...
private string _DisplayName;
public string DisplayName {
get { return _DisplayName; }
set { SetProperty(ref _DisplayName, value, "DisplayName"); }
}
protected virtual string GetTargetName() {
return "panelHost";
}
#region IMVVMDockingProperties Members
string IMVVMDockingProperties.TargetName {
get { return GetTargetName(); }
set { throw new NotImplementedException(); }
}
#endregion
}
public class DockItemTemplateSelector : DataTemplateSelector {
public DataTemplate LayoutPanelTemplate { get; set; }
public DataTemplate DocumentPanelTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container) {
return item is DocumentViewModel ? DocumentPanelTemplate : LayoutPanelTemplate;
}
}
}
See Also
Choosing Templates Based on Custom Logic