左键单击 Silverlight DataGrid 的奇怪问题
在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
点击事件不会冒泡。如果子控件将事件标记为已处理,它将停止。
在本例中,左键单击被 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.