0

I have WPF application with Canvas and a Ruler. Now I need to pass Canvas OnMouseMove(MouseEventArgs e) to the Ruler to reflect mouse movements on its scale. Both controls are created independently while initialization, they do not know of each other. How can I transfer the mouse position on the Canvas to the Ruler?

4

2 回答 2

0

我找到了解决方案,但我仍然有疑问。在我看来有点复杂:

  1. Canvas 作为“父”控件包含以下代码:

    公共点 MousePosition { get { return (Point)GetValue(MousePositionProperty); } set { SetValue(MousePositionProperty, value); } }

    public static readonly DependencyProperty MousePositionProperty =
            DependencyProperty.Register("MousePosition", typeof(Point), typeof(DesignerCanvas), new UIPropertyMetadata(point));
    
    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);
        MousePosition = e.GetPosition(this);
    

    }

  2. 标尺包含以下代码:

    public Point Position { get { return (Point)GetValue(PositionProperty); } set { SetValue(PositionProperty, value); } }

    public static readonly DependencyProperty PositionProperty =
        DependencyProperty.Register("Position", typeof(Point), typeof(HorizontalRuler), new UIPropertyMetadata(defaultMousePoint,
          new PropertyChangedCallback(PositionPropertyChangedCallback)));
    
    private static void PositionPropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        HorizontalRuler horizontalRuler = (HorizontalRuler)sender;
        horizontalRuler.InvalidateVisual();
    }
    
于 2012-10-08T08:42:48.340 回答
0

MouseEventArgs 有一个GetPosition方法,允许您将任何 IInputElement 作为参数传递,并返回鼠标指针相对于指定元素的位置。将标尺作为参数值传递。

于 2012-10-07T18:09:45.887 回答