Hi there, How can i use ZoomExtents() on MVVM ?
- che1024 asked 8 years ago
- You must login to post comments
che1024 (and Andrew):
I’d like to offer an alternative, although I’m unsure if it’s appropriate since it violates the view-viewmodel decoupling.
In your ViewModel, make a static delegate, or an action
public class YourViewModel
{
// This can be an event, if you prefer
public static Action UpdateChartElements;
public ObservableCollection<IRenderableSeriesViewModel> renderableSeriesList { get; set; }
// Rest of class
// method that plots data
public void plotData()
{
// if you are creating multiple renderable series, make sure you invoke
// this action OUTSIDE a for or foreach loop
foreach //...
{
renderableSeriesList.Add(...) // your renderable series
}
// This is C# 6.0 code. If you're using anything before that, make sure you're
// checking that your delegate handlers are not null before you invoke this action
UpdateChartElements?.Invoke();
}
}
In your codebehind, you can assign a method to the action:
public class YourViewCodeBehind
{
// Assigning the method to the delegate handler in the constructor
public YourViewCodeBehind()
{
InitializeComponent();
// Note that because your action is static, it assigns the method on the class level
ICPComparisonViewModel.UpdateChartElements += zoomExtentsMethod;
}
public void zoomExtentsMethod()
{
Dispatcher.Invoke(DispatcherPriority.Render, new Action(() =>
{
// You can add more UI stuff you want to update here
// Make sure you label your chart in XAML
yourChart.ZoomExtents();
}));
}
}
By calling the dispatcher, which is the UI thread, you are awaiting everything before rendering to be completed before the chart refreshes, which is why I would say be careful and make sure you’re not putting the dispatch invoke in the middle of a loop or between any possible CPU-intensive tasks.
Hope this helps!
— Ari
- Ari Sagiv answered 8 years ago
- last edited 8 years ago
-
Take a look at https://blogs.msdn.microsoft.com/davidrickard/2010/03/31/using-the-dispatcher-with-mvvm/ — it may simplify your code and make it a bit cleaner! Principle is correct though. Invoking on the dispatcher (acually, I would use BeginInvoke for async invoke on UI thread) will ensure SciChart has a chance to draw before zooming to fit
-
Thanks for the tip!
- You must login to post comments
Hi there,
We have an article on various MVVM API’s here.
In order to ZoomExtents() in MVVM the simplest way is to call DataSeries.InvalidateParentSurface(RangeMode.ZoomToFit).
Best regards,
Andrew
- Andrew Burnett-Thompson answered 8 years ago
-
Andrew — This doesn’t seem to work for me. I have a RenderableSeriesViewModel bound to a chart, and I would call this function on the renderableSeries’ “DataSeries” parameter (Maybe i’m doing it wrong). Is there an event that I can raise when the renderableSeries data is changed so that I can just do a ZoomExtents from the codebehind or a DelegateCommand? — Ari
-
Hi Ari, There is a known problem where if you update data in ViewModel then immediately call DataSeries.InvalidateParentSurface() it will not zoom to fit — why? Because SciChart has not yet received the data (via WPF’s binding). The best workaround at the moment is to invoke DataSeries.InvalidateParentSurface after a short time period, e.g. 100ms. Best regards, Andrew
- You must login to post comments
Please login first to submit.