Hi,
I have a XamDataChart inside a MainWindow scroll Viewer and the scrollViewer implements below to scroll the contents with Mouse Scroll.
private void OnPreviewMouseWheel (object sender, MouseWheelEventArgs e){ScrollViewer scrollView = (ScrollViewer) sender;scrollView.ScrollToVerticalOffset (scrollView.VerticalOffset - e.Delta);e.Handled = true;}
The issue is when cursor comes on XamDataChart while Mouse Scroll, I want chart should zoom in/zoom out as it works on Mouse scroll but this is not happening.
When I remove the above code, I can see chartzoom in/zoom out on Mouse scroll, but window contents are not scrolling.
I have set HorizontalZoomable and VerticalZoomable for chart to true.
Any comments?
Thanks,
Preeti
Hello Preeti,
I have been investigating this behavior you are reporting, and by marking e.Handled = true in your OnPreviewMouseWheel event handler for your ScrollViewer in this case, you will essentially be preventing the mouse-wheel event from being handled in the XamDataChart. This will effectively prevent the mouse-wheel zoom operations from happening.
Is there a particular reason that you are handling this PreviewMouseWheel event on the ScrollViewer and marking it handled?
Please let me know if you have any other questions or concerns on this matter.
Hello,
Yes...My window contains different UserControl which includes chart.
without e.handled=true, I am not able to keep the cursor anywhere on window and scroll up and down. I want this behavior with the scrollViewer.
But I also want that when while scrolling the cursor comes to chart or when I click on the chart, the chart should zoom. If I click outside the chart, window content should scroll.
Does that clarify the problem?
Thank you for clarifying the issue in this case, but if you set the e.Handled property to true in the PreviewMouseWheel event of your ScrollViewer, you will not be able to zoom the chart with the mouse-wheel. The mouse-wheel events will essentially be "eaten" by this statement, and it will never get to the chart.
The default behavior of the XamDataChart is that it will detect mouse-wheel events when the mouse is over the chart, and zoom it as long as the mouse is over the plot area. Perhaps you could check the XamDataChart.IsMouseOver property for true in your PreviewMouseWheel event and do not do anything there if that property returns true? For your "click in the chart" functionality, you could also check the XamDataChart.IsFocused property for true in your PreviewMouseWheel event as well.
I hope this helps. Please let me know if you have any other questions or concerns on this matter.
Hi Andrew,
Thanks for the suggestion. This worked.
I implemented below code to make it work.
private void OnPreviewMouseWheel (object sender, MouseWheelEventArgs e) { ScrollViewer scrollView = (ScrollViewer) sender; var chart = GetDescendantByType (scrollView, typeof (XamDataChart)); if (chart != null && ((XamDataChart)chart).IsMouseOver) e.Handled = false; else { scrollView.ScrollToVerticalOffset (scrollView.VerticalOffset - e.Delta); e.Handled = true; } }
public static Visual GetDescendantByType(Visual element, Type type) { if (element == null) return null; if (element.GetType() == type) return element; Visual foundElement = null;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++) { Visual visual = VisualTreeHelper.GetChild(element, i) as Visual; foundElement = GetDescendantByType(visual, type); if (foundElement != null) break; } return foundElement; }
Now I am able to both the views (window contents and Chart zooming).
Thanks again.