OnMouseMove不会在WPF中的画布上触发

Ign*_*cia 13 c# wpf performance events

我已经完成了自定义图表控件,我想在光标后面绘制一个简单的交叉.该图表在Canvas上实现为PolyLine,我在Canvas的OnMouseMove事件中绘制两条线来改变它们的坐标.

The surprise was to found that the event get called only each 10 seconds or so event when the MainGUI thread is idle (the UI is completely responsive and if I pause the application the main thread is at the App mainForm.ShowDialog()).

Any idea on how to find why is this happening? I get the same performance using the OnMouseMove or the PreviewOnMouseMove.

EDIT: This is how I paint the cross (anyway if I put a breakpoint at the OnMouseMove it only stops from time to time).

XAML:

<Border BorderThickness="1" BorderBrush="White" Grid.Column="1" Grid.Row="0" Grid.RowSpan="2" Grid.ColumnSpan="2" >
    <Canvas x:Name="DrawArea" PreviewMouseMove="DrawArea_PreviewMouseMove" />
</Border>
Run Code Online (Sandbox Code Playgroud)

CS:

 public Chart()
 {
    _line = new Polyline();
    _line.Stroke = Brushes.Orange;
    _crossX = new Line();
    _crossY = new Line();
    _crossX.Stroke = Brushes.Orange;
    _crossY.Stroke = Brushes.Orange;
    _crossX.StrokeThickness = 1;
    _crossY.StrokeThickness = 1;

    InitializeComponent();

    this.DrawArea.Children.Add(_line);
    this.DrawArea.Children.Add(_crossX);
    this.DrawArea.Children.Add(_crossY);
}     

private void DrawArea_MouseMove(object sender, MouseEventArgs e)
{
    Point mousePosition = System.Windows.Input.Mouse.GetPosition(this.DrawArea);

    _crossX.X1 = 0;
    _crossX.X2 = this.DrawArea.ActualWidth;
    _crossX.Y1 = _crossX.Y2 = mousePosition.Y;

    _crossY.Y1 = 0;
    _crossY.Y2 = this.DrawArea.ActualHeight;
    _crossY.X1 = _crossY.X2 = mousePosition.X;
}
Run Code Online (Sandbox Code Playgroud)

WPF*_*-it 28

这很奇怪,我不知道为什么......

FrameworkElement.MouseMove 仅当区域有一些明确的背景画笔\填充集时才有效.

在您的情况下设置Canvas.Background="Transparent",它应该工作.

还有另一个解决方法...... WPF在CaptureMouse()之后不发送MouseMove事件;

这可能是因为它HitTest取决于彩色像素及其反馈.

无论是什么,它都没有在MSDN上记录下来,并且让许多UI设计者感到困惑.

  • 我只有两个字:谢谢和WTF ???? 真的很奇怪.有人应该填补微软的缺陷吗? (2认同)
  • 如果画布上有一个孩子,比如形状,文本块或其他什么,则会为该对象和画布触发鼠标事件(它们是直接事件,因此它们不会通过从子项中冒泡而到达画布).这非常奇怪. (2认同)
  • 这也适用于`Grid`. (2认同)