阻止ScrollViewer处理鼠标滚轮事件

时间:2015-10-22 08:14:13

标签: c# .net wpf scrollviewer uielement

有没有办法防止ScrollViewer处理鼠标滚动?

以下代码是不够的,因为事件应由ScrollViewer的子元素处理:

private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
     e.Handled = true;
}

EDIT - @Anders Keller Carstensen

我曾经直接实例化PlotModel对象,这就是为什么我没有看到AddHandler()方法。我现在改变了我的代码,但情节并没有显示任何内容:

public OxyPlot.Wpf.PlotView Plot { get; set; }

public MainWindow()
{
    InitializeComponent();
    DataContext = this;

    Plot = new OxyPlot.Wpf.PlotView();
    Plot.Model = new PlotModel();

    Plot.AddHandler(System.Windows.UIElement.PreviewMouseWheelEvent, new MouseWheelEventHandler(Plot_PreviewMouseWheel), true);

    // Create Line series
    var s1 = new LineSeries();
    s1.Points.Add(new DataPoint(2, 7));
    s1.Points.Add(new DataPoint(7, 9));
    s1.Points.Add(new DataPoint(9, 4));

    // add Series and Axis to plot model
    Plot.Model.Series.Add(s1);
    Plot.Model.InvalidatePlot(false);
}

private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
    e.Handled = true;
}

private void Plot_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
    MessageBox.Show("Button_PreviewMouseWheel");
}

这是相应的XAML:

<ScrollViewer Margin="0,25,0,0" PreviewMouseWheel="ScrollViewer_PreviewMouseWheel">
    <oxy:PlotView Model="{Binding Plot}" Height="2000" Margin="0,0,15,0" />
</ScrollViewer>

我确定Model="{Binding Plot}"错了,但我不知道要写什么。

1 个答案:

答案 0 :(得分:2)

编辑 - 这是一个更好的解决方案。

您可以在ScrollViewer的PreviewMouseWheel事件处理程序中设置e.Handled = true,然后仍在其他控件中处理该事件。

例如,假设我在ScrollViewer的内容中有一个网格内的按钮。在窗口的构造函数中,我订阅了按钮的PreviewMouseWheel事件,如下所示:

public ScrollViewerWindow() {
  InitializeComponent();

  btn.AddHandler(UIElement.PreviewMouseWheelEvent, 
                 new MouseWheelEventHandler(Button_PreviewMouseWheel), 
                 true // Handler will be called even though e.Handled = true
                );
}

然后您的事件句柄将如下所示:

private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e) {
  e.Handled = true;
}

private void Button_PreviewMouseWheel(object sender, MouseWheelEventArgs e) {
  MessageBox.Show("Button_PreviewMouseWheel");
}

这是对您的新问题的回答。

在PAMView中为PlotView命名,并将Model绑定到PlotModel属性。

<ScrollViewer Margin="0,25,0,0" PreviewMouseWheel="ScrollViewer_PreviewMouseWheel">
    <oxy:PlotView x:Name="pv" Model="{Binding PlotModel}" Height="2000" Margin="0,0,15,0" />
</ScrollViewer>

在您的代码隐藏中,您应该添加PlotModel属性并对其进行初始化:

public PlotModel PlotModel { get; set; }

public MainWindow()
{
    InitializeComponent();
    DataContext = this;

    pv.AddHandler(System.Windows.UIElement.PreviewMouseWheelEvent, new MouseWheelEventHandler(Plot_PreviewMouseWheel), true);

    // Create Line series
    var s1 = new LineSeries();
    s1.Points.Add(new DataPoint(2, 7));
    s1.Points.Add(new DataPoint(7, 9));
    s1.Points.Add(new DataPoint(9, 4));

    // add Series and Axis to plot model
    PlotModel = new PlotModel();
    PlotModel.Series.Add(s1);
    PlotModel.InvalidatePlot(false);
}

private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
    e.Handled = true;
}

private void Plot_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
    MessageBox.Show("Plot_PreviewMouseWheel");
}

旧解决方案:

<击> 创建一个对所有HitTest请求做出否定响应的ScrollViewer子类似乎可以解决您的问题 - 但是,它可能会产生您不喜欢的副作用。检查一下,看看它是否符合您的需求。

首先,创建子类:

public class MyScrollViewer : ScrollViewer {
  protected override HitTestResult HitTestCore(PointHitTestParameters hitTestParameters) {
    return null;
  }
}

然后,在XAML中,将<ScrollViewer>更改为<local:MyScrollViewer>

确保声明了本地命名空间。它看起来像这样:

<Window ....
        xmlns:local="clr-namespace:WPFTest">

<击>