Simple shapes in DiagramControl display only their content as an editable string. If it’s necessary to add custom elements to diagram items, use the DiagramContentItem class. DiagramContentItem has the DiagramContentItem.ContentTemplate property, which can be used to put any elements into the item.
<Style x:Key="formattedTextContentItem" TargetType="dxdiag:DiagramContentItem">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
...
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
To register DiagramContentItem in the toolbox, use the DiagramStencil.RegisterTool method with FactoryItemTool as a parameter:
stencil.RegisterTool(new FactoryItemTool(
"Calendar",
() => "Calendar",
diagram => new DiagramContentItem() {
CustomStyleId = "formattedTextContentItem"
},
new Size(230, 110), true));
After that, register the stencil using DiagramToolboxRegistrator.RegisterStencil.
Please note that to properly deserialize DiagramContentItem, it’s necessary to set its CustomStyleId property, which accepts a key of a Style applied to the item.
The following example shows how to create diagram items that display custom content.
View Example
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Namespace DXDiagram.ContentItem
Public Class ButtonItemModel
Public Sub ShowMessage()
MessageBox.Show("This command has been created by our POCO machanism based on the ShowMessage method")
End Sub
End Class
End Namespace
<Window x:Class="DXDiagram.ContentItem.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:dxdiag="http://schemas.devexpress.com/winfx/2008/xaml/diagram"
mc:Ignorable="d"
Title="MainWindow" Height="700" Width="1200">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="DiagramResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<dxdiag:DiagramDesignerControl Name="diagramControl" SelectedStencils="BasicShapes" />
</Grid>
</Window>
Imports System
Imports System.Collections.Generic
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
Imports DevExpress.Diagram.Core
Imports DevExpress.Xpf.Core
Imports DevExpress.Xpf.Diagram
Imports DevExpress.Mvvm.POCO
Namespace DXDiagram.ContentItem
Partial Public Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
RegisterContentItemTools()
LoadDiagram()
End Sub
Private Sub RegisterContentItemTools()
Dim stencil As New DiagramStencil("CustomTools", "Content Item Tools")
stencil.RegisterTool(New FactoryItemTool("Text", Function() "Text", Function(diagram) New DiagramContentItem() With {.CustomStyleId = "formattedTextContentItem"}, New Size(230, 110), True))
stencil.RegisterTool(New FactoryItemTool("Logo", Function() "Logo", Function(diagram) New DiagramContentItem() With {.CustomStyleId = "devExpressLogoContentItem"}, New Size(230, 80), True))
stencil.RegisterTool(New FactoryItemTool("Action", Function() "Button", Function(diagram) New DiagramContentItem() With {.CustomStyleId = "buttonContentItem"}, New Size(230, 80), True))
DiagramToolboxRegistrator.RegisterStencil(stencil)
End Sub
Private Sub LoadDiagram()
diagramControl.DocumentSource = "DiagramData.xml"
diagramControl.SelectedStencils = New StencilCollection(New String() { "CustomTools" })
End Sub
End Class
End Namespace
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxdiag="http://schemas.devexpress.com/winfx/2008/xaml/diagram"
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
xmlns:local="clr-namespace:DXDiagram.ContentItem"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Style x:Key="formattedTextContentItem" TargetType="dxdiag:DiagramContentItem">
<Setter Property="Background" Value="#FF35779C"/>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock TextWrapping="Wrap" TextAlignment="Center" VerticalAlignment="Center" Margin="10">
DiagramContentItem allows you to use custom content in diagram items. For example, you can use it for displaying
<Run FontSize="14" Foreground="#FFF3F3A8" FontWeight="Bold">formatted</Run>
text.
</TextBlock>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="devExpressLogoContentItem" TargetType="dxdiag:DiagramContentItem">
<Setter Property="Background" Value="#FFEC7D32"/>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel HorizontalAlignment="Center" Margin="10" VerticalAlignment="Center">
<TextBlock Text="Item with several images" VerticalAlignment="Center" FontSize="20"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Image Source="Images\DevExpressLogo.png" MaxHeight="32" Margin="5,0,0,0"/>
<Image Source="Images\Globe.png" MaxHeight="32" />
<Image Source="Images\Apply.png" MaxHeight="32" />
</StackPanel>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="buttonContentItem" TargetType="dxdiag:DiagramContentItem">
<Setter Property="Background" Value="Gray"/>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10" DataContext="{dxmvvm:ViewModelSource Type={x:Type local:ButtonItemModel}}">
<TextBlock Text="Item with a command" VerticalAlignment="Center" FontSize="20"/>
<Button Content="Show additional info" Command="{Binding ShowMessageCommand}" MaxHeight="40" Margin="5,0,0,0" Cursor="Hand"/>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
using System;
using System.Collections.Generic;
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;
using DevExpress.Diagram.Core;
using DevExpress.Xpf.Core;
using DevExpress.Xpf.Diagram;
using DevExpress.Mvvm.POCO;
namespace DXDiagram.ContentItem {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
RegisterContentItemTools();
LoadDiagram();
}
void RegisterContentItemTools() {
DiagramStencil stencil = new DiagramStencil("CustomTools", "Content Item Tools");
stencil.RegisterTool(new FactoryItemTool(
"Text",
() => "Text",
diagram => new DiagramContentItem() {
CustomStyleId = "formattedTextContentItem"
},
new Size(230, 110), true));
stencil.RegisterTool(new FactoryItemTool(
"Logo",
() => "Logo",
diagram => new DiagramContentItem() {
CustomStyleId = "devExpressLogoContentItem",
},
new Size(230, 80), true));
stencil.RegisterTool(new FactoryItemTool(
"Action",
() => "Button",
diagram => new DiagramContentItem()
{
CustomStyleId = "buttonContentItem",
},
new Size(230, 80), true));
DiagramToolboxRegistrator.RegisterStencil(stencil);
}
void LoadDiagram() {
diagramControl.DocumentSource = "DiagramData.xml";
diagramControl.SelectedStencils = new StencilCollection(new string[] { "CustomTools" });
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
namespace DXDiagram.ContentItem
{
public class ButtonItemModel
{
public void ShowMessage()
{
MessageBox.Show("This command has been created by our POCO machanism based on the ShowMessage method");
}
}
}