Pre loader

FAQ: How to call SciChartSurface.SuspendUpdates() in a ViewModel

Answered Closed

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.

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?

  • You must to post comments
Best Answer

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

  1. 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; }
  2. 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);
  3. Set the attached property in your view. This even works inside SciChartGroup

    <s:SciChartGroup ItemsSource="{Binding ChartPaneViewModels}">
                        <!-- A lot has been omitted for brevity here. Just set the property on the SciChartSurface -->          
                        <s:SciStockChart multiPane:Injector.PassSurfaceToViewModel="True">
  4. When the application starts, the SciChartSurface (or SciStockChart) will be passed to the ViewModel, cast to ISuspendable.

You can now call SuspendUpdates() directly 🙂

  • Andrew Chin
    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
  • Andrew Burnett-Thompson
    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
  • Stefan Rado
    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 to post comments
Showing 1 result

Try SciChart Today

Start a trial and discover why we are the choice
of demanding developers worldwide

Start TrialCase Studies