Pre loader

Zoom with key stroke not working the first time

Welcome to the SciChart Forums!

  • Please read our Question Asking Guidelines for how to format a good question
  • Some reputation is required to post answers. Get up-voted to avoid the spam filter!
  • We welcome community answers and upvotes. Every Q&A improves SciChart for everyone

WPF Forums | JavaScript Forums | Android Forums | iOS Forums

0
0

Dear all,

I have use the sample code below in order to be able to scroll with key stroke instead of mouse

public class SimpleZoomInOutModifier : ChartModifierBase

{
    public static readonly DependencyProperty ZoomFractionProperty
        = DependencyProperty.Register(“ZoomFraction”, typeof(double), typeof(SimpleZoomInOutModifier), new PropertyMetadata(0.1));
 
    public double ZoomFraction
    {
        get { return (double)GetValue(ZoomFractionProperty); }
        set { SetValue(ZoomFractionProperty, value); }
    }
 
    void SciChart_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        double factor = 0;
 
        if (e.Key == Key.Up)
        {
            // On CTRL+, Zoom In
            factor = -ZoomFraction;
        }
        if (e.Key == Key.Down)
        {
            // On CTRL-, Zoom Out
            factor = ZoomFraction;
        }
 
        using (ParentSurface.SuspendUpdates())
        {
            // Zoom the XAxis by the required factor
            XAxis.ZoomBy(factor, factor, TimeSpan.FromMilliseconds(500));
 
            // Zoom the YAxis by the required factor
            YAxis.ZoomBy(factor, factor, TimeSpan.FromMilliseconds(500));
 
            // Note.. can be extended for multiple YAxis XAxis, just iterate over all axes on the parent surface
        }
    }
 
    public override void OnAttached()
    {
        base.OnAttached();
        var scichart = ((SciChartSurface)ParentSurface);
 
        var mainWindow = FindLogicalParent(scichart);
 
        mainWindow.PreviewKeyDown -= SciChart_PreviewKeyDown;
        mainWindow.PreviewKeyDown += SciChart_PreviewKeyDown;
        mainWindow.Loaded += SciChart_Loaded;
    }
 
    private void SciChart_Loaded(object sender, RoutedEventArgs e)
    {
        double factor = 0;
        // On CTRL+, Zoom In
        factor = -ZoomFraction;
 
        using (ParentSurface.SuspendUpdates())
        {
            // Zoom the XAxis by the required factor
            XAxis.ZoomBy(factor, factor, TimeSpan.FromMilliseconds(500));
 
            // Zoom the YAxis by the required factor
            YAxis.ZoomBy(factor, factor, TimeSpan.FromMilliseconds(500));
 
            // Note.. can be extended for multiple YAxis XAxis, just iterate over all axes on the parent surface
        }
    }

The problem I have is that when my chart load and I press the key stroke for zooming in, it does not work.
TO make it work I need to zoom once with mouse and then I can use zoom in/out key after without problem.

Any idea how to solve that behavior ?

regards

Version
4
  • You must to post comments
0
0

My guess is that the ScIChartSurface is not focussed so the keyboard events are not being passed to the SciChartSurface.

You have to click on the chart to focus it.

Or, you can programmatically focus by calling ((MainGrid)SciChartSurface.RootGrid).Focus().

Finally, if you want to handle key events globally then you have to do this at a higher level in your app. WPF handles key events hierachically, so only the focused element and its children will receive key events.

  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.

Try SciChart Today

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

Start TrialCase Studies