WPF Canvas的PreviewMouseWheel事件处理程序没有';t触发事件
本文关键字:事件 Canvas PreviewMouseWheel 事件处理 程序 WPF | 更新日期: 2023-09-27 18:28:17
我开发了一个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"画布用于绘制图表,并具有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根本不启动或很少启动。之前,我为"chartCcanvas"使用了MouseWheel事件处理程序,并得到了相同的结果。这个错误的原因是什么。请帮忙!
您需要将chartCanvas Background设置为Transparent。
<Canvas Name="chartCanvas" ClipToBounds="True" PreviewMouseWheel="chartCanvas_PreviewMouseWheel" Background="Transparent"/>