wpf-devexpress-dot-xpf-dot-charts-dot-axis2d-a3cc7422.md
Gets or sets the custom logic for selecting a data template that converts a model object to a custom label.
Namespace : DevExpress.Xpf.Charts
Assembly : DevExpress.Xpf.Charts.v25.2.dll
NuGet Package : DevExpress.Wpf.Charts
public DataTemplateSelector CustomLabelItemTemplateSelector { get; set; }
Public Property CustomLabelItemTemplateSelector As DataTemplateSelector
| Type | Description |
|---|---|
| DataTemplateSelector |
A DataTemplateSelector descendant that applies a template based on custom logic.
|
To implement custom logic to choose a template, create a DataTemplateSelector descendant and override the SelectTemplate method. This method returns a data template for the specified axis label item.
The following code generates custom axis labels. The label template is selected based on the SelectTemplate method’s returned value.
<Window.DataContext>
<viewModel:ChartViewModel/>
</Window.DataContext>
<dxc:ChartControl>
<dxc:XYDiagram2D>
<dxc:XYDiagram2D.AxisY>
<dxc:AxisY2D CustomLabelItemsSource="{Binding CustomLabels}">
<dxc:AxisY2D.CustomLabelItemTemplateSelector>
<local:CustomLabelSelector>
<local:CustomLabelSelector.AboveLevel>
<DataTemplate>
<dxc:CustomAxisLabel Value="{Binding AxisValue}">
<dxc:CustomAxisLabel.Content>
<TextBlock FontSize="20"
Foreground="Green"
Text="{Binding SourceObject.LabelContent}">
</TextBlock>
</dxc:CustomAxisLabel.Content>
</dxc:CustomAxisLabel>
</DataTemplate>
</local:CustomLabelSelector.AboveLevel>
<local:CustomLabelSelector.BelowLevel>
<DataTemplate>
<dxc:CustomAxisLabel Value="{Binding AxisValue}">
<dxc:CustomAxisLabel.Content>
<TextBlock FontSize="15"
Foreground="Red"
Text="{Binding Value}">
</TextBlock>
</dxc:CustomAxisLabel.Content>
</dxc:CustomAxisLabel>
</DataTemplate>
</local:CustomLabelSelector.BelowLevel>
</local:CustomLabelSelector>
</dxc:AxisY2D.CustomLabelItemTemplateSelector>
</dxc:AxisY2D>
</dxc:XYDiagram2D.AxisY>
</dxc:XYDiagram2D>
</dxc:ChartControl>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
// ChartViewModel
public class ChartViewModel
{
public ObservableCollection<LabelItem> CustomLabels { get; set; }
public ChartViewModel()
{
CustomLabels = new ObservableCollection<LabelItem>();
CustomLabels.Add(new LabelItem() { AxisValue = 5, LabelContent = "5++" });
CustomLabels.Add(new LabelItem() { AxisValue = 10, LabelContent = "10++"});
CustomLabels.Add(new LabelItem() { AxisValue = 15, LabelContent = "15**" });
}
}
// CustomLabelSelector
class CustomLabelSelector : DataTemplateSelector {
public DataTemplate BelowLevel { get; set; }
public DataTemplate AboveLevel { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container) {
LabelItem axisLabel = item as LabelItem;
if (axisLabel == null)
return null;
return axisLabel.AxisValue >= 10 ? AboveLevel : BelowLevel;
}
}
// LabelItem
public class LabelItem
{
public double AxisValue { get; set; }
public string LabelContent { get; set; }
}
Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
' ChartViewModel
Public Class ChartViewModel
Public Property CustomLabels As ObservableCollection(Of LabelItem)
Public Sub New()
CustomLabels = New ObservableCollection(Of LabelItem)()
CustomLabels.Add(New LabelItem() With {
.AxisValue = 5,
.LabelContent = "5++"
})
CustomLabels.Add(New LabelItem() With {
.AxisValue = 10,
.LabelContent = "10++"
})
CustomLabels.Add(New LabelItem() With {
.AxisValue = 15,
.LabelContent = "15**"
})
End Sub
End Class
' CustomLabelSelector
Friend Class CustomLabelSelector
Inherits DataTemplateSelector
Public Property BelowLevel As DataTemplate
Public Property AboveLevel As DataTemplate
Public Overrides Function SelectTemplate(ByVal item As Object, ByVal container As DependencyObject) As DataTemplate
Dim axisLabel As LabelItem = TryCast(item, LabelItem)
If axisLabel Is Nothing Then Return DirectCast(Nothing, DataTemplate)
Return If(axisLabel.AxisValue >= 10, AboveLevel, BelowLevel)
End Function
End Class
' LabelItem
Public Class LabelItem
Public Property AxisValue As Double
Public Property LabelContent As String
End Class
See Also