Silverlight KeyDown 事件的行为
在我的 Silverlight 4 DataGrid
控件中,我想附加一个非常简单的行为,该行为在按键时执行自定义命令 - 实际上,在按下 ENTER 键时提交 DataGrid 中的选定项目。
虽然行为实际上有效(请参阅我的代码...
//.... in "OnAttached()..."
this.AssociatedObject.AddHandler(Control.KeyDownEvent, new KeyEventHandler(OnKeyDown), true);
private void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
InvokeCommand();
}
}
...),但我遇到了问题,DataGrid 似乎可以自行处理 ENTER 键按下并继续到下一行。显然,发生的情况是提交了错误的行,因为当我处理按键时,行选择已经更改。
这是 XAML:
<data:DataGrid
AutoGenerateColumns="False"
IsReadOnly="True"
ItemsSource="{Binding Path=Data}"
SelectedItem="{Binding SelectedRow, Mode=TwoWay}">
<data:DataGrid.Columns>
<data:DataGridTextColumn Binding="{Binding A}" />
<data:DataGridTextColumn Binding="{Binding B}" />
<data:DataGridTextColumn Binding="{Binding C}" />
</data:DataGrid.Columns>
<i:Interaction.Behaviors>
<behaviors:EnterBehavior Command="{Binding CommitCommand}" />
</i:Interaction.Behaviors>
</data:DataGrid>
您能告诉我如何防止默认的 ENTER 事件吗?
In my Silverlight 4 DataGrid
control, I wanted to attach a very simple Behavior which executes a custom command on key Press - actually, commit the selected item in the DataGrid on ENTER key press.
While the Behavior actually works (see my code...
//.... in "OnAttached()..."
this.AssociatedObject.AddHandler(Control.KeyDownEvent, new KeyEventHandler(OnKeyDown), true);
private void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
InvokeCommand();
}
}
...) I have the problem, that the DataGrid seems to handle the ENTER key press itself and proceeds to the next row. Obviously, what happens is that the wrong Row is committed, because when I handle the Key Press, the row selectedion has already changed.
Here is the XAML:
<data:DataGrid
AutoGenerateColumns="False"
IsReadOnly="True"
ItemsSource="{Binding Path=Data}"
SelectedItem="{Binding SelectedRow, Mode=TwoWay}">
<data:DataGrid.Columns>
<data:DataGridTextColumn Binding="{Binding A}" />
<data:DataGridTextColumn Binding="{Binding B}" />
<data:DataGridTextColumn Binding="{Binding C}" />
</data:DataGrid.Columns>
<i:Interaction.Behaviors>
<behaviors:EnterBehavior Command="{Binding CommitCommand}" />
</i:Interaction.Behaviors>
</data:DataGrid>
Can you tell me how I can prevent the default ENTER event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
猜猜现在帮助OP有点晚了,但我通过子类化数据网格并覆盖 KeyDown 方法以将 e.Handled 设置为 true 解决了这个问题。这将停止 DataGrid 的默认输入处理,然后您自己的操作即可生效。
(显然,您必须将 XAML 中的 DataGrid 实例替换为 YourCustomDataGrid)
Guess it's a bit late now to help the OP, but I solved this by subclassing the data grid and overriding the KeyDown method to set e.Handled to true. That stops the default enter processing of the DataGrid, then your own actions can take effect.
(Obviously you have to replace instances of DataGrid in the XAML with YourCustomDataGrid)
不要依赖
SelectedRow
,使用首先引发事件的行作为提交操作的参数。参见下面的代码:Don't rely on
SelectedRow
, use the row that raised the event in the first place as a parameter for your submit operation. See code below:查看是否使用 AddHandler 重载 handlerEventsToo 可以在这里为您提供帮助。在某些情况下,这允许您调用处理程序,即使先前的处理程序已经设置handled=true。
See if using the AddHandler overload with handledEventsToo can help you here. In certain cases, this allows you to get your handler invoked even if a previous handler already set handled=true.