【发布时间】:2015-12-28 07:09:57
【问题描述】:
我开发了一个 WPF UserControl,它有两个 Canvas 实例——一个在另一个(用户控件是使用 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 开发的。此应用程序使用上面提到的用户控件。
<!--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 事件处理程序并得到相同的结果之前。这个错误的原因是什么。请帮忙!
【问题讨论】:
-
这可能与画布完全点击通过鼠标命中测试不可见有关。尝试在画布上设置 Background="Transparent"。
-
为 chartCanvas 设置背景="透明"?
-
我已经为 chartCanvas 坐了 Background="Transparent" 并且现在所有的作品。谢谢!
-
不客气,也请采纳正确答案,以备日后参考。
标签: c# wpf mvvm user-controls caliburn.micro