2015-12-28 54 views
0

我开发了一个WPF UserControl,它有两个Canvas实例 - 一个在另一个实例中(Usercontrol是使用Caliburn.Micro开发的)。下面是UserControl XAML。WPF画布的PreviewMouseWheel事件处理程序不会触发事件

<UserControl x:Class="ChartControl.LineChart" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:local="clr-namespace:ChartControl" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 

    <!--Main layout greed.--> 
    <Grid Margin="10"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="Auto"/> 
     <ColumnDefinition Width="*"/> 
    </Grid.ColumnDefinitions> 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 
    <!--Layout greed for canvases--> 
    <Grid Margin="0,0,0,0" x:Name ="chartGrid" Grid.Column="1" Grid.Row="1" 
      ClipToBounds="False" Background="Transparent" SizeChanged="chartGrid_SizeChanged"> 
     <!-- Height and Width of this Canvas are equal to Heigt and Width of Greed where this canvase is--> 
     <Canvas Margin="2" Name="textCanvas" Grid.Column="1" Grid.Row="1" ClipToBounds="True" Background="Aqua" 
      Width="{Binding ElementName=chartGrid, Path=ActualWidth}" Height="{Binding ElementName=chartGrid, this Path=ActualHeight}"> 
      <Canvas Name="chartCanvas" ClipToBounds="True" PreviewMouseWheel="chartCanvas_PreviewMouseWheel"/> 
     </Canvas> 
    </Grid> 
</Grid> 
</UserControl> 

附带的“chartCanvas”画布用于图表绘制并具有PreviewMouseWheel事件处理程序。

private void chartCanvas_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e) 
{ 
    double dx = e.Delta; 
    double dy = e.Delta; 
    // Change X min coordinate of the chart. 
    _chartStyle.Xmin = _chartStyle.Xmin + (_chartStyle.Xmax - _chartStyle.Xmin) * dx/chartCanvas.Width; 
    // Change X max coordinate of the chart. 
    _chartStyle.Xmax = _chartStyle.Xmax - (_chartStyle.Xmax - _chartStyle.Xmin) * dx/chartCanvas.Width; 
    // Change Y min coordinate of the chart. 
    _chartStyle.Ymin = _chartStyle.Ymin + (_chartStyle.Ymax - _chartStyle.Ymin) * dy/chartCanvas.Height; 
    // Change X nax coordinate of the chart. 
    _chartStyle.Ymax = _chartStyle.Ymax - (_chartStyle.Ymax - _chartStyle.Ymin) * dy/chartCanvas.Height; 
    // Prepair canvases to redrawing. 
    chartCanvas.Children.Clear(); 
    textCanvas.Children.RemoveRange(1, textCanvas.Children.Count - 1); 
    // Draw vertical and horizontal grid lines. 
    _chartStyle.AddChartStyle(tbTitle, tbXLabel, tbYLabel); 
    // Draw the chart curve. 
    _chartStyle.SetLines(DataCollection); 
} 

在相同的解决方案,其中UserControl是我有一个WPF应用程序。这是MVVM应用程序,并使用Caliburn.Micro开发。这个应用程序使用上面提到的Usercontrol。

<!--The Applicaion main view--> 
<UserControl x:Class="ChartDrawer.Views.MainWindowView" 
. . . . . . . . . . . . . . . . 
    <!--Here the usercontrol is included in the application--> 
    xmlns:local="clr-namespace:ChartControl;assembly=ChartControl" 
. . . . . . . . . . . . . . . . 
> 
    . . . . . . . . . . . . . . . . 
    <!--Here is the binding of application properties as sources to the Usercontrol properties as targets--> 
    <local:LineChart Grid.Row="3" Grid.Column="0" DataCollection="{Binding DataCollection}" Xmin="{Binding Xmin}" Xmax="{Binding Xmax}" XTick="{Binding XTick}" Ymin="{Binding Ymin}" Ymax="{Binding Ymax}" YTick="{Binding YTick}"/> 
    . . . . . . . . . . . . . . . . 
</UserControl> 

我的问题如下。当我运行我的应用程序时,单击图表曲线(绘制在chartCanvas上)并旋转鼠标滚轮,然后ChartCanvas_PreviewMouseWheel根本不会触发或很少触发。在为“chartCanvas”使用MouseWheel事件处理程序之前,它具有相同的结果。这个错误的原因是什么?请帮忙!

+0

它可能与canvas完全点击后通过鼠标命中测试不可见。尝试在画布上设置背景=“透明”。 –

+0

为chartCanvas设置背景=“透明”? – user3769902

+0

我已经为chartCanvas和所有作品现在坐了Background =“Transparent”。谢谢! – user3769902

回答

1

您需要将chartCanvas Background设置为Transparent。

<Canvas Name="chartCanvas" ClipToBounds="True" PreviewMouseWheel="chartCanvas_PreviewMouseWheel" Background="Transparent"/> 
相关问题