Knowledgebase: Tips and Tricks
Axis GridlinesStyle DynamicResource or Style Binding Doesn't Work
Posted by Andrew BT on 26 March 2014 10:51 PM

The problem

Are are experiencing problems trying to style the Axis MajorGridLines or MinorGridLines using DynamicResource, or style binding?

Typically, if you follow our guidelines for styling in our Xaml Styling example, then everything would work fine!

The Xaml Styling example. Mmmm so beautiful ...

An example of a working Xaml style for Axis Gridlines is below:

<Style TargetType="s:AxisBase">
   <Setter Property="MajorGridLineStyle">
		<Setter.Value>
			<Style TargetType="Line">
				<Setter Property="Stroke" Value="Red"/>
				<Setter Property="StrokeThickness" Value="1"/>
				<Setter Property="StrokeDashArray" Value="10 5"/>
			</Style>
		</Setter.Value>
	</Setter>
</Style>

But what if you wanted to dynamically update the style?

If you wanted to dynamically update the GridLinesColour, using a Style Binding, or Dynamic Resource then you will experience problems. The following code won't work for instance

<visuals:ColorToBrushConverter x:Key="ColorToBrushConverter"/>
<Style TargetType="visuals:AxisBase">
	<Setter Property="TickTextBrush" Value="{Binding TextColor, Converter={StaticResource ColorToBrushConverter}}"/>
	<Setter Property="MajorGridLineStyle">
		<Setter.Value>
			<Style TargetType="Line">
				<!-- This won't work! Dynamic resources or style bindings won't update -->
				<Setter Property="Stroke" Value="{Binding Path=LineColor, Converter={StaticResource ColorToBrushConverter}}"/>
				<!-- Or this also won't work -->
				<Setter Property="Stroke" Value="{DynamicResource GridLineStroke}"/>
			</Style>
		</Setter.Value>
	</Setter>
</Style>

Why Doesn't a DynamicResource or Style Binding Work when styling Axis Gridlines?

The Grid Lines in SciChart are not WPF UIElements. They are rendered on a bitmap and the style is parsed and converted for use in our bitmap rendering engine. Once the original style changes we cannot listen to updates so cannot update the bitmap rendering engine.

 

 

The Workaround

What you are going to have to do is create and set a new style in code when you want the colour to change. There is an article here on how to create a style in Code  

One of our users bound the MajorGridLineStyle to a Color property and used a Converter to return a new style each time. Try that, let us know if it works! 

(2 vote(s))
Helpful
Not helpful

CONTACT US

Not sure where to start? Contact us, we are happy to help!


CONTACT US

SciChart Ltd, 16 Beaufort Court, Admirals Way, Docklands, London, E14 9XL. Email: Legal Company Number: 07430048, VAT Number: 101957725