We keep getting asked What’s the best practice way to suspend the SciChartSurface in a multi-pane stock chart demo.
We have multiple ways of doing this.
- You can call SuspendUpdates on a DataSeries
- You can lock the parent surface SyncRoot etc…
- You can call SuspendUpdates on the SciChartSurface instance
By far the most effective and thread-safe way to do this is to call SuspendUpdates directly on the SciChartSurface. But how to do this in a ViewModel?
- Andrew Burnett-Thompson asked 8 years ago
- last edited 8 years ago
- You must login to post comments
We know this breaks MVVM, but we recommend in really high performance scenarios, passing the SciChartSurface directly to your viewmodel and calling SciChartSurface.SuspendUpdates().
The interface that has SuspendUpdates() is actually ISuspendable. SciChartSurface implements ISuspendable.
// SciChartSurface implements ISciChartSurface and ISuspendable interfaces
public class SciChartSurface : ISciChartSurface, ISuspendable
{
// ...
In order to not break testability of your ViewModels, we recommend you cast to ISuspendable before passing your SciChartSurface to a ViewModel. Then, you can call SuspendUpdates directly (on any thread) and you can still unit test your ViewModels.
Worked Example – Create Multi Pane Stock Charts example
Create a property in your chart pane ViewModel to hold the ISuspendable
public abstract class BaseChartPaneViewModel : BaseViewModel, IChildPane { private ISuspendable _scs; public ISuspendable Suspendable { get { return _scs; } set { _scs = value; } }
Create an Attached Property to inject the SciChartSurface as ISuspendable
public class Injector { public static readonly DependencyProperty PassSurfaceToViewModelProperty = DependencyProperty.RegisterAttached( "PassSurfaceToViewModel", typeof(bool), typeof(Injector), new PropertyMetadata(default(bool), OnPassSurfaceToViewModelPropertyChanged)); private static void OnPassSurfaceToViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var scs = d as SciChartSurface; if (scs == null || ((bool)e.NewValue) == false) return; scs.DataContextChanged += (s, arg) => UpdateProperty(scs, scs.DataContext as BaseChartPaneViewModel); UpdateProperty(scs, scs.DataContext as BaseChartPaneViewModel); } private static void UpdateProperty(SciChartSurface scs, BaseChartPaneViewModel vm) { if (vm == null) return; vm.Suspendable = scs; } public static void SetPassSurfaceToViewModel(DependencyObject element, ISuspendable value) { element.SetValue(PassSurfaceToViewModelProperty, value); } public static bool GetPassSurfaceToViewModel(DependencyObject element) { return (bool)element.GetValue(PassSurfaceToViewModelProperty); } }
Set the attached property in your view. This even works inside SciChartGroup
<s:SciChartGroup ItemsSource="{Binding ChartPaneViewModels}"> <s:SciChartGroup.ItemTemplate> <DataTemplate> <!-- A lot has been omitted for brevity here. Just set the property on the SciChartSurface --> <s:SciStockChart multiPane:Injector.PassSurfaceToViewModel="True">
When the application starts, the SciChartSurface (or SciStockChart) will be passed to the ViewModel, cast to ISuspendable.
You can now call SuspendUpdates() directly 🙂
- Andrew Burnett-Thompson answered 8 years ago
- last edited 8 years ago
- Hi Andrew, Is there any way possible to bind SciChart instance through DI to ViewModel. I’m kind of stuck in between DI(Dagger2) and SciChart objects in VM through ISuspendable. With data-binding in Fragment & VM with constructor argument of ISuspendable is not possible ahead of time. Please suggest. Regards, Praween
- Hi Andrew, the above post is for WPF not android or Xamarin — Do you want to open a question in our Android or iOS forum for our tech team to take a look at? Click ‘ask’ at https://www.scichart.com/questions/
- Wouldn’t it be easier to just have an attached property that can be bound to a view model which suspends updates if set to true? I know this doesn’t give the flexibility of having multiple disposable suspend locks, but that could be handled internally in the view model if really needed.
- You must login to post comments