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
- sc sc asked 6 years ago
- You must login to post comments
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.
- Andrew Burnett-Thompson answered 6 years ago
- You must login to post comments
Please login first to submit.