Silverlight KeyDown 事件的行为

发布于 2024-10-17 02:25:11 字数 1207 浏览 3 评论 0原文

在我的 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 技术交流群。

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

发布评论

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

评论(3

淡淡離愁欲言轉身 2024-10-24 02:25:12

猜猜现在帮助OP有点晚了,但我通过子类化数据网格并覆盖 KeyDown 方法以将 e.Handled 设置为 true 解决了这个问题。这将停止 DataGrid 的默认输入处理,然后您自己的操作即可生效。

(显然,您必须将 XAML 中的 DataGrid 实例替换为 YourCustomDataGrid)

public class YourCustomDataGrid : DataGrid
{
    protected override void OnKeyDown(KeyEventArgs e)
    {
        // Stop "Enter" selecting the next row in the grid
        if (e.Key == Key.Enter)
        {
            e.Handled = true;
        }
        base.OnKeyDown(e);
    }
}

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)

public class YourCustomDataGrid : DataGrid
{
    protected override void OnKeyDown(KeyEventArgs e)
    {
        // Stop "Enter" selecting the next row in the grid
        if (e.Key == Key.Enter)
        {
            e.Handled = true;
        }
        base.OnKeyDown(e);
    }
}
十秒萌定你 2024-10-24 02:25:12

不要依赖 SelectedRow,使用首先引发事件的行作为提交操作的参数。参见下面的代码:

private void OnKeyDown(object sender, KeyEventArgs e) 
{ 
    if (e.Key == Key.Enter) 
    { 
        InvokeCommand(e.OriginalSource); 
    }
}

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:

private void OnKeyDown(object sender, KeyEventArgs e) 
{ 
    if (e.Key == Key.Enter) 
    { 
        InvokeCommand(e.OriginalSource); 
    }
}
他不在意 2024-10-24 02:25:12

查看是否使用 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.

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