WPF Canvas的PreviewMouseWheel事件处理程序不会在事件

时间:2015-12-28 07:09:57

标签: c# wpf mvvm user-controls caliburn.micro

我开发了一个WPF UserControl,它有两个Canvas实例 - 一个在另一个实例中(Usercontrol是使用Caliburn.Micro开发的)。以下是UserControl XAML。

<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”Canvas用于图表绘制,并具有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根本不会触发或很少触发。之前我使用MouseWheel事件处理程序“chartCanvas”并具有相同的结果。这个错误的原因是什么?请帮忙!

1 个答案:

答案 0 :(得分:1)

您需要将chartCanvas背景设置为透明。

if ledColorList[0] == "red":
    blink(7)