左键单击 Silverlight DataGrid 的奇怪问题

发布于 2024-12-05 01:43:10 字数 761 浏览 1 评论 0原文

在使用 Silverlights DataGrid 时,我遇到了一些非常奇怪的鼠标事件行为:

我想要做的只是当用户在我的 DataGrid 上单击鼠标左键时调用某种方法。这应该不是什么大问题,但是......

public void doLeftClick (object sender, MouseButtonEventArgs e) {
    // some code
}

定义了 EventHandler 并将

myDataGrid.MouseLeftButtonDown += doLeftClick;

其附加到事件。

其结果是,只有当我左键单击 DataGrid 的一列时,才会调用 doLeftClick 方法!

当我只为右键单击而不是左键单击执行与上面完全相同的代码时,每次我右键单击 DataGrid 时,无论鼠标光标在哪里,只要它位于 DataGrid 的边界内,就会调用 EventHandler控制(这是我实际需要的左键单击以及我期望从此设置中获得的行为):

public void doRightClick (object sender, MouseButtonEventArgs e) {
    // some code
}

myDatagrid.MouseRightButtonDown += doRightClick;

那么我做错了什么?我忘记了什么? 我真的很感激任何帮助:)

马克

I am experiencing some very strange mouse-event behaviour when working with Silverlights DataGrid:

What I want to do is simply call some method when the user left-clicks over my DataGrid. That shouldn't be much a problem, but ...

With

public void doLeftClick (object sender, MouseButtonEventArgs e) {
    // some code
}

i am defining the EventHandler and with

myDataGrid.MouseLeftButtonDown += doLeftClick;

i am attaching it to the event.

The result of that is that the doLeftClick method only gets called when i left-click over one of the columns of my DataGrid!

When i am doing the exact same code as above only for the right-click instead of the left-click the EventHandler gets called everytime i right-click over my DataGrid regardless where the mouse cursor is, as long it is inside the boundaries of the control (which is what i actually need with the left-click and what's the behavior i would expect from this setting):

public void doRightClick (object sender, MouseButtonEventArgs e) {
    // some code
}

myDatagrid.MouseRightButtonDown += doRightClick;

So what am i doing wrong ? What am i forgetting ?
I really would appreciate any help :)

Marc

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

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

发布评论

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

评论(1

最丧也最甜 2024-12-12 01:43:10

点击事件不会冒泡。如果子控件将事件标记为已处理,它将停止。

在本例中,左键单击被 DataGrid 单元格占用(以便选择它们和/或将焦点集中到编辑控件)。

单元格不以相同的方式使用右键单击,因此会向上传播到 DataGrid 控件。

列标题足够好,可以让左键单击传播。

The click events are not bubbled up. If a child control marks the event as handled it stops.

In this instance the left click is being eaten by the DataGrid cells (in order to select them and/or give focus to edit controls).

Right click is not used by the cells in the same way, so propagates up to the DataGrid control.

The column headers are nice enough to allow the left click to propagate.

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