wpf - 数据网格按钮列

标签 wpf mvvm datagrid

我想创建一个带有 ButtonDataGridColumn 。看起来很简单吧?

仅:

  • 客户应该能够使用箭头导航网格列。
  • 当其中包含 ButtonDataGridGolumn 获得焦点时,按 Enter 键应激活该命令。 (例如不需要按 Tab 键即可将焦点放在按钮上)
  • 使用 Enter空格 单击按钮

我尝试了 DataGridTemplateColumnCellTemplate:

<DataGridTemplateColumn CellStyle="{StaticResource ResourceKey=Button}">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button Style="{StaticResource LinkButton}" FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <i:InvokeCommandAction Command="Delete" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <Image Source="Delete.ico"/>
            </Button>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

最佳答案

如果您希望在 Cell 获得焦点后立即将按钮聚焦,则应在 DataGridCell 上将 KeyboardNavigation.IsTabStop 设置为 False -

<DataGridTemplateColumn CellStyle="{StaticResource ResourceKey=Button}">
    <DataGridTemplateColumn.CellTemplate>
        ....
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
        </Style>
     </DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>

因此,当您按 Tab 时,它会发现 DataGridCell 不是此处的停止点,因此移动到下一个制表位,这将是您的 Button.

您可以在资源中创建Style,并在任何需要相同行为的地方使用它。

关于wpf - 数据网格按钮列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13589368/

相关文章:

c++ - 二维插值

c# - DataGrid 获取选中行的列值

wpf - 资源字典中的 DataTemplate 不继承自 app.xaml 样式?

.net - PropertyChangedEventManager 何时创建以及何时附加?

wpf mvvm .. 访问 View 模型中的 View 元素

wpf - 通过 setter v ContentTemplate 设置内容样式

wpf - 在 DataGrid 中捕获 DoubleClick

wpf - WPF 资源中的整数值?

c# - 在 MVVM WPF 中打开新窗口

c# - 为什么 ViewModelLocator 成员不是静态的