SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, and now iOS Charting & Android Chart Components

Answered Closed
0
0

On priority support tickets, a user just asked us how to show and hide the AxisTitle TextBlock without changing the AxisTitle.Text to string.Empty.

The solution uses attached properties and a small custom style, so we thought we’d post it below

  • You must to post comments
Best Answer
1
0

The solution is to use the Axis TitleStyle to control visibillity of the axis title text block via an attached property.

Start out by creating this code:

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace WpfApplication2
{
    // Define attahced property
    public class AxisExtensions
    {
        public static readonly DependencyProperty DrawAxisTitleProperty = DependencyProperty.RegisterAttached(
            "DrawAxisTitle", typeof(bool), typeof(AxisExtensions), new PropertyMetadata(true));

        public static void SetDrawAxisTitle(DependencyObject element, bool value)
        {
            element.SetValue(DrawAxisTitleProperty, value);
        }

        public static bool GetDrawAxisTitle(DependencyObject element)
        {
            return (bool)element.GetValue(DrawAxisTitleProperty);
        }
    }
}

XAML

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:s="http://schemas.abtsoftware.co.uk/scichart"
        xmlns:common="clr-namespace:WpfApplication2"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="b2v"></BooleanToVisibilityConverter>
        <Style x:Key="BottomAxisTitleStyle" TargetType="s:AxisTitle">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding}"
                                   Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=s:DateTimeAxis}, Path=(common:AxisExtensions.DrawAxisTitle), Converter={StaticResource b2v}}"/>
                    </DataTemplate>
                </Setter.Value>
            </Setter>                                    
        </Style>
    </Window.Resources>
    <Grid>
        <s:SciChartSurface>
            <s:SciChartSurface.XAxis>
                <s:DateTimeAxis AxisTitle="Hello World!" TitleStyle="{StaticResource BottomAxisTitleStyle}" common:AxisExtensions.DrawAxisTitle="True"/>
            </s:SciChartSurface.XAxis>
            <s:SciChartSurface.YAxis>
                <s:NumericAxis/>
            </s:SciChartSurface.YAxis>
        </s:SciChartSurface>
    </Grid>
</Window>

That’s it!

enter image description here

The attached property AxisExtensions adds a boolean property to the Axis itself. Next, the declared Axis TitleStyle has a TextBlock with Text-value bound to its data context (in this case the axis title string), and Visibility set to a RelativeSource binding up to the AxisExtensions.DrawAxisTitle attached property.

For more information on axis part templating, please see our article titled Styling Axis Labels, Axis Titles, Rotating Axis Labels

  • You must to post comments
Showing 1 result