Problem in switching dynamically the colorTheme property

Need help in implementing some specific function to your LightningChart Ultimate powered application? Post a question and get code snippets from other LightningChart Ultimate community members.

Moderator: Arction_LasseP

Posts: 1
Joined: Mon Oct 19, 2020 10:59 pm

Problem in switching dynamically the colorTheme property

Post by cchrc » Mon Nov 02, 2020 9:09 am

In my application users can switch theme between dark and light at runtime. So I would like that also the charts follow the same behavior, however I'm struggling in achieving this.
I'm using the semi-bindable API, so I have a xaml like this:

Code: Select all

		ColorTheme="{Binding Theme.Value}"
                Title="{Binding ChartTitle.Value}"
                ActiveView="{Binding ViewType.Value}">
However this approach do not work. When ColorTheme binding is present I get a null reference exception:
System.NullReferenceException: Object reference not set to an instance of an object.
at Arction.Wpf.SemibindableCharting.LightningChartUltimate.AQC(ColorTheme A)
at Arction.Wpf.SemibindableCharting.LightningChartUltimate.ColorThemeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue)
at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.Activate(Object item)
at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
at System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance)
at MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance)
at MS.Internal.Data.DataBindEngine.Run(Object arg)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)

A more complete stacktrace produced by ChartChartMessage event can be found at:

So I then followed the approach described in this topic: viewtopic.php?f=16&t=653&p=2439&hilit=colorTheme#p2439
However it does not work very well as the result is a partial theme change as you can see in the attached screenshot. It seems that some part of the chart are not rendered with the correct theme.

Thanks in advance.
theme not working correctly
theme not working correctly
theme_problem.png (33.79 KiB) Viewed 196 times

Posts: 105
Joined: Wed Mar 27, 2019 1:05 pm

Re: Problem in switching dynamically the colorTheme property

Post by Arction_LasseP » Mon Nov 02, 2020 9:58 am


This is a known issue. Currently, you can't set ColorTheme properly in xaml because of the creation order of chart components. Views such as ViewXY are created with default color settings after the chart itself has been created in the xaml. Therefore the ColorTheme is overridden by the default colors. This is something we will fix in a future release.

The workaround is to set the theme in the code behind as is suggested in the thread you mentioned. Why it affects only partially in your app is unclear. Are you modifying some colors after this? Or maybe the binding affects this somehow.
You could also change the ColorTheme inside Loaded or even AfterRendering event to make sure the chart has been loaded first. At least in my tests these worked.

Code: Select all

_chart.Loaded += _chart_Loaded;

        private void _chart_Loaded(object sender, RoutedEventArgs e)
            _chart.ColorTheme = ColorTheme.LightGray;
// or alternatively

_chart.AfterRendering += _chart_AfterRendering;

        private void _chart_AfterRendering(object sender, AfterRenderingEventArgs e)
            _chart.AfterRendering -= _chart_AfterRendering; // To make the event run only once.
            _chart.ColorTheme = ColorTheme.LightGray;
Hope this helps.
Best regards,