wpf 通过命令绑定获取点击位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12642645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:35:39  来源:igfitidea点击:

Get click position over command binding

c#wpfmvvmcommand

提问by beta vulgaris

What I want to do is a UserControl containing a section with a grid, where something happens when clicking on the grid. I need the position of the pixel where the click happened and I'm doing all of this MVVM style. I know how I can prompt actions on my ViewModel:

我想要做的是一个 UserControl 包含一个带有网格的部分,点击网格时会发生一些事情。我需要点击发生的像素的位置,我正在做所有这些 MVVM 风格。我知道如何在我的 ViewModel 上提示操作:

<Grid>
 <Grid.InputBindings>
   <MouseBinding Gesture="LeftClick" Command="{Binding MinimapClick}"/>
 </Grid.InputBindings>
</Grid>

My problem is now that I don't know how to retrieve the Coordinates... any ideas? I appreciate your help!

我的问题是现在我不知道如何检索坐标......有什么想法吗?我感谢您的帮助!

采纳答案by Kevin DiTraglia

How about this?

这个怎么样?

private void MinimapClick(object parameter)
{
    Point mousePos = Mouse.GetPosition(myWindow);
}

If you don't have a reference to the window you could send it as a parameter (or use whatever reference point you want).

如果您没有对窗口的引用,您可以将其作为参数发送(或使用您想要的任何参考点)。

回答by beta vulgaris

KDiTraglia had the right pointerfor me... In any case I had some issues with defining the actions and binding to my ViewModel. I'll post my solution in case someone else has some problems. Here's what I've done in the xaml:

KDiTraglia为我提供了正确的指针......无论如何,我在定义操作和绑定到我的 ViewModel 时遇到了一些问题。如果其他人有问题,我会发布我的解决方案。这是我在 xaml 中所做的:

<Grid Width="100" Height="100" Grid.Column="2" Grid.Row="2" x:Name="TargetGrid">
    <Grid>
        <Grid.InputBindings>
            <MouseBinding Gesture="LeftClick" Command="{Binding Path=TargetClick}" CommandParameter="{Binding ElementName=TargetGrid}" />
        </Grid.InputBindings>
    </Grid>
</Grid>

I create the UserControl and bind it to the ViewModel. In the ViewModel I implement and create the following command:

我创建了 UserControl 并将其绑定到 ViewModel。在 ViewModel 中,我实现并创建了以下命令:

public class PositioningCommand : ICommand
{
    public PositioningCommand()
    {
    }

    public void Execute(object parameter)
    {
        Point mousePos = Mouse.GetPosition((IInputElement)parameter);
        Console.WriteLine("Position: " + mousePos.ToString());
    }

    public bool CanExecute(object parameter) { return true; }

    public event EventHandler CanExecuteChanged;
}

public PositioningCommand TargetClick
{
    get;
    internal set;
}

回答by ActualRandy

I had a related issue - I wanted to capture the mouse position for click events for my ContextMenu. Problem: my menu's parent(an image control) was not recognized by the CommandParameter ElementName.

我有一个相关的问题 - 我想为我的ContextMenu捕获单击事件的鼠标位置。问题:CommandParameter ElementName 无法识别我的菜单的父级(图像控件)。

For reference, the binding error I received, priorto adding my menu to the namespace, was:

作为参考,在将菜单添加到命名空间之前,我收到的绑定错误是:

    System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=imgArena'. BindingExpression:(no path); DataItem=null; target element is 'MenuItem' (Name='mnuAddItem'); target property is 'CommandParameter' (type 'Object')

Apparently WPF context menus belong to a different visual tree than your control, making binding quite frustrating.

显然,WPF 上下文菜单与您的控件属于不同的可视化树,这使得绑定非常令人沮丧。

After some research, I found this simple fix, which I placed in the constructor for my code behind:

经过一番研究,我找到了这个简单的修复程序,我将其放置在后面代码的构造函数中:

    NameScope.SetNameScope(mnuGrid, NameScope.GetNameScope(this));

Where "mnuGrid" is the name of my context menu.

其中“mnuGrid”是我的上下文菜单的名称。

After I did this, I was able to pass my control as a parameter to my command exactly as Beta Vulgaris did above.

完成此操作后,我能够像上面的 Beta Vulgaris 一样将我的控件作为参数传递给我的命令。

For reference, my XAML looks like this:

作为参考,我的 XAML 如下所示:

    <Image Name="imgArena" >
        <Image.ContextMenu>
            <ContextMenu Name="mnuGrid">
                <MenuItem Header="Place _Entry" Name="mnuAddItem"
                    Command="{Binding AddEntryCmd}" 
                    CommandParameter="{Binding ElementName=imgArena}" />
            </ContextMenu>
        <Image.ContextMenu>
    </Image>