New to Telerik UI for WPF? Download free 30-day trial

Getting Started with WPF Legend

This tutorial will walk you through the creation of a sample application that contains RadLegend.

Assembly References

In order to use RadLegend, you will need to add a reference to the Telerik.Windows.Controls assembly.

You can find the required assemblies for each control from the suite in the Controls Dependencies help article.

Adding Telerik Assemblies Using NuGet

To use RadLegend when working with NuGet packages, install the Telerik.Windows.Controls.for.Wpf.Xaml package. The package name may vary slightly based on the Telerik dlls set - Xaml or NoXaml

Read more about NuGet installation in the Installing UI for WPF from NuGet Package article.

Defining RadLegend and Adding Items

The RadLegend control is populated with data via its Items collection. The collection is of type LegendItemCollection and can be populated only with LegendItem objects. By default the Items property is null.

Example 1: Defining RadLegend in XAML

<telerik:RadLegend> 
    <telerik:RadLegend.Items> 
        <telerik:LegendItemCollection> 
            <telerik:LegendItem MarkerFill="#FF55AA33" Title="Legend item 1" /> 
            <telerik:LegendItem MarkerFill="#FFCC3399" Title="Legend item 2" /> 
            <telerik:LegendItem MarkerFill="#FF5511BB" Title="Legend item 3" /> 
        </telerik:LegendItemCollection> 
    </telerik:RadLegend.Items> 
</telerik:RadLegend> 

Example 2: Defining RadLegend in code

RadLegend legend = new RadLegend(); 
legend.Items = new LegendItemCollection() 
{ 
    new LegendItem() { Title = "Legend item 1", MarkerFill = new SolidColorBrush((Color)ColorConverter.ColorFromString("#FF55AA33")) }, 
    new LegendItem() { Title = "Legend item 2", MarkerFill = new SolidColorBrush((Color)ColorConverter.ColorFromString("#FFCC3399")) }, 
    new LegendItem() { Title = "Legend item 3", MarkerFill = new SolidColorBrush((Color)ColorConverter.ColorFromString("#FF5511BB")) }, 
}; 

Figure 1: RadLegend

WPF RadLegend RadLegend

Customizing the Legend Items

The most straightforward way to customize the legend items is to set the properties of the LegendItem objects added in the Items collection of the control.

To change the fill of the item's marker set the MarkerFill property of the LegendItem object.

To change the text of the items set the Title property of the LegendItem object.

To change the geometry of the marker set the MarkerGeometry property. Read about this in the Marker Geometry article.

The control also allows you to fully replace its items' template and define a custom visualization. To do so, set the ItemTemplate property of the RadLegend control. Read more about this in the ItemTemplate article.

Changing the Items Panel

By default the RadLegend control uses a StackPanel to render its items. To change this you can replace the panel via the ItemsPanel property. Read more about this in the ItemsPanel article.

Telerik UI for WPF Learning Resources

See Also

In this article