Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

Axis2D.CustomLabelItemTemplateSelector Property

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.v24.2.dll

NuGet Package: DevExpress.Wpf.Charts

#Declaration

public DataTemplateSelector CustomLabelItemTemplateSelector { get; set; }

#Property Value

Type Description
DataTemplateSelector

A DataTemplateSelector descendant that applies a template based on custom logic.

#Remarks

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.

#Example

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; }
}
See Also