xaml 中的鼠标位置 - wpf

发布于 2024-08-25 16:26:59 字数 407 浏览 1 评论 0原文

是否可以将鼠标的位置指定为 XAML 中 Command 的 Parameter 属性的值。像下面这样:

<UserControl.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Create Link"
                      Command="{Binding CreateLink}"
                      CommandParameter="{Binding Mouse.Position}" >
            </MenuItem>
        </ContextMenu>
</UserControl.ContextMenu>

Is it possible to specify the position of the mouse as the value of the Parameter property of a Command in XAML. Something like the following:

<UserControl.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Create Link"
                      Command="{Binding CreateLink}"
                      CommandParameter="{Binding Mouse.Position}" >
            </MenuItem>
        </ContextMenu>
</UserControl.ContextMenu>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天的雪花 2024-09-01 16:26:59

在 WPF 中,没有 Mouse.Position。如果您查看 鼠标类成员,最接近的是Mouse.GetPosition,返回相对于某个 UI 元素的位置。

我建议使用 UI 元素本身作为命令参数,并让您的命令调用 Mouse.GetPosition 来检索位置(如果您的 DataContext 出于某种原因需要这样做)。

In WPF, there is no Mouse.Position. If you look at the Mouse class members, the closest thing is Mouse.GetPosition, which returns the position relative to some UI element.

I would recommend using the UI element itself as the Command Parameter, and having your Command call Mouse.GetPosition to retrieve positioning, if this is required in your DataContext for some reason.

掌心的温暖 2024-09-01 16:26:59

是的,如果您希望以编程方式执行此操作,则可以使用 PointToScreen() 以及 Mouse.GetPosition(this) 来获取绝对鼠标点。如果您需要以编程方式而不是在 XAML 中执行此操作,请查看此内容,

http://ivolo.mit.edu/post/WPF-Mouse-and-Point-Acrobatics.aspx

Yeah, if you were looking to do it programatically, you can use the PointToScreen() as well as the Mouse.GetPosition(this) to get the absolute mouse point. Take a look at this should you need to do this programatically, not in XAML,

http://ivolo.mit.edu/post/WPF-Mouse-and-Point-Acrobatics.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文