簡體   English   中英

使用 XamlBehaviorsWpf 和 MVVM 時如何獲取鼠標 position(以前的交互性)

[英]How to get mouse position when using XamlBehaviorsWpf and MVVM (Formerly Interactivity)

Now that XamlBehaviorsWpf [Tag missing, but I don't have the rights to add] has replaced System.Windows.Interactivity in Blend SDK which is deprecated I'm struggling to figure out how to use it to get Mouse position on a canvas.

我認為下面的MouseXMouseY是舊的Interactivity語法,但我似乎找不到XamlBehaviorsWpf等效項。 因此,使用這些屬性會產生編譯錯誤。

Click方法正在工作,並以其默認值 999,999 返回坐標。

有誰知道我如何使用XamlBehaviorsWpf

XAML:

        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas  Height="800" Background="Black">
                    <behaviors:Interaction.Triggers>
                        <behaviors:EventTrigger EventName="MouseLeftButtonDown">
                            <behaviors:CallMethodAction TargetObject="{Binding}" MethodName="Click"/>
                        </behaviors:EventTrigger>
                        <behaviors:EventTrigger EventName="MouseMove">
                            <behaviors:ChangePropertyAction MouseX="{Binding X, Mode=OneWayToSource}"/>
                            <behaviors:ChangePropertyAction MouseY="{Binding Y, Mode=OneWayToSource}"/>
                        </behaviors:EventTrigger>
                    </behaviors:Interaction.Triggers>
                </Canvas>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

視圖模型

    public void Click(object sender, MouseButtonEventArgs e)
    {
        MessageBox.Show(X.ToString() + ","+ Y.ToString());
    }

    private double x = 999;

    public double X
    {
        get { return x; }
        set { SetAndNotify(ref this.x, value); }
    }

    private double y = 999;

    public double Y
    {
        get { return y; }
        set { SetAndNotify(ref this.y, value); }
    }

我的很多問題似乎都源於一個錯字,已在問題中得到糾正(感謝安迪)。 這里有一篇易於理解的文章,我曾經讓它工作。 您基本上可以將所有System.Windows.Interactivity引用與XamlBehaviorsWpf交換,它的工作原理都是一樣的。

我的工作代碼如下以便於參考,或者如果上面的鏈接消失:

XAML

<UserControl x:Class="Drain.Views.Cad.CadView"
             x:Name="cad"
             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:behaviors="http://schemas.microsoft.com/xaml/behaviors"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Drain.Views.Cad"
             xmlns:vm="clr-namespace:Drain.ViewModels.Cad"
             xmlns:cad="clr-namespace:Drain.Models.Cad"
             xmlns:s="https://github.com/canton7/Stylet"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        
        
        <behaviors:Interaction.Behaviors>
            <behaviors:FluidMoveBehavior AppliesTo="Children" />
        </behaviors:Interaction.Behaviors>
        
        <ItemsControl x:Name="MainPanel">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas  Height="800" Background="Black">
                        <behaviors:Interaction.Triggers>
                            <behaviors:EventTrigger EventName="MouseLeftButtonDown">
                                <behaviors:CallMethodAction TargetObject="{Binding}" MethodName="Click"/>
                            </behaviors:EventTrigger>
                        </behaviors:Interaction.Triggers>
                        <behaviors:Interaction.Behaviors>
                            <vm:MouseBehaviour MouseX="{Binding X, Mode=OneWayToSource}"
                                               MouseY="{Binding Y, Mode=OneWayToSource}" />
                        </behaviors:Interaction.Behaviors>
                    </Canvas>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemContainerStyle>
                <Style>
                    <Setter Property="Canvas.Left" Value="{Binding Path=X, Mode=OneWay}" />
                    <Setter Property="Canvas.Top" Value="{Binding Path=Y, Mode=OneWay}" />
                </Style>
            </ItemsControl.ItemContainerStyle>
        </ItemsControl>
    </Grid>
</UserControl>

視圖模型

    public void Click(object sender, MouseButtonEventArgs e)
    {
        MessageBox.Show(X.ToString() + ","+ Y.ToString());
    }

    private double x = 999;

    public double X
    {
        get { return x; }
        set { SetAndNotify(ref this.x, value); }
    }

    private double y = 999;

    public double Y
    {
        get { return y; }
        set { SetAndNotify(ref this.y, value); }
    }

鼠標行為 Class

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Xaml.Behaviors;

namespace Drain.ViewModels.Cad
{
   public class MouseBehaviour : Behavior<Panel>
    {
        public static readonly DependencyProperty MouseYProperty = DependencyProperty.Register(
           "MouseY", typeof(double), typeof(MouseBehaviour), new PropertyMetadata(default(double)));

        public static readonly DependencyProperty MouseXProperty = DependencyProperty.Register(
           "MouseX", typeof(double), typeof(MouseBehaviour), new PropertyMetadata(default(double)));

        public double MouseY
        {
            get { return (double)GetValue(MouseYProperty); }
            set { SetValue(MouseYProperty, value); }
        }

        public double MouseX
        {
            get { return (double)GetValue(MouseXProperty); }
            set { SetValue(MouseXProperty, value); }
        }

        protected override void OnAttached()
        {
            AssociatedObject.MouseMove += AssociatedObjectOnMouseMove;
        }

        private void AssociatedObjectOnMouseMove(object sender, MouseEventArgs mouseEventArgs)
        {
            var pos = mouseEventArgs.GetPosition(AssociatedObject);
            MouseX = pos.X;
            MouseY = pos.Y;
        }

        protected override void OnDetaching()
        {
            AssociatedObject.MouseMove -= AssociatedObjectOnMouseMove;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM