【问题标题】:Select Datagrid row using Spacebar in Silverlight application在 Silverlight 应用程序中使用空格键选择 Datagrid 行
【发布时间】:2015-11-20 06:43:17
【问题描述】:

我在 Silverlight 应用程序中有一个 Datagrid。用户可以使用 Tab 键将焦点放在 Datagrid 上,并使用向上和向下箭头键在各行之间移动。

请告知,当用户按下空格键选择行时如何触发行选择事件。

下面是sn-p的代码:

<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>
...

【问题讨论】:

    标签: c# xaml silverlight datagrid


    【解决方案1】:

    显然解决方案非常简单。

    第 1 步:将 KeyDown 添加到 Datagrid。

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

    步骤 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();
        }
    }
    

    【讨论】:

      【解决方案2】:

      试试这个:

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

      您可以将选定的值绑定到视图模型中的属性。

      【讨论】:

      • 感谢瑞恩的帮助。但是,在 XAML 文件中添加上述行后,我看到一个错误“无法识别或无法访问成员“InputBindings”。我已添加代码 sn-p 以供参考。
      • 是否有必要 100% 使用 而不是
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-02
      • 2010-12-11
      • 1970-01-01
      • 2013-06-02
      • 2011-11-27
      • 1970-01-01
      • 2010-10-16
      相关资源
      最近更新 更多