简体   繁体   English

在Silverlight应用程序中使用空格键选择Datagrid行

[英]Select Datagrid row using Spacebar in Silverlight application

I have a Datagrid in Silverlight application. 我在Silverlight应用程序中有一个Datagrid。 The user is able to get the focus on Datagrid using Tab key and move between various rows using Up and Down arrow Key. 用户可以使用Tab键将注意力集中在Datagrid上,并使用向上和向下箭头键在各行之间移动。

Please advice, how to fire row select event when the user hits the Spacebar Key for an selected row. 请提出建议,当用户按下选定行的空格键时,如何触发行选择事件。

Below is the code snippet: 下面是代码片段:

<Custom:ClientControl  
x:Class="TestNamespace.Modules.Views.SampleView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
mc:Ignorable="d"  
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" 
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
<sdk:DataGrid x:Name="dg" ...>
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseLeftButtonUp">
      <i:InvokeCommandAction Command="{Binding DoSomething}" />
    </i:EventTrigger>
  </i:Interaction.Triggers>
<sdk:DataGrid.Columns>
...

Apprently the solution turned out to be very smiple. 显然,解决方案非常简单。

Step 1: Add KeyDown to the Datagrid. 步骤1:将KeyDown添加到Datagrid。

<sdk:DataGrid x:Name="dg" KeyDown="dg_KeyDown">

Step 2: In .XAML.CS file inside Datagrid KeyDown event invoke the method which handles MouseLeftButtonUp event. 步骤2:在Datagrid KeyDown事件内的.XAML.CS文件中,调用处理MouseLeftButtonUp事件的方法。

private void dg_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
    if (e.Key == System.Windows.Input.Key.Space)
    {
        this.viewModel.DoSomething();
    }
}

Try this: 尝试这个:

<DataGrid>
    <DataGrid.InputBindings>
         <KeyBinding Key="Space" Command="{Binding DoSomething}"/>
    </DataGrid.InputBindings>
</DataGrid>

You can bind the selected value to a property in your View model. 您可以将所选值绑定到View模型中的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM