我想创建一个带有 Button
的 DataGridColumn
。看起来很简单吧?
仅:
- 客户应该能够使用箭头导航网格列。
- 当其中包含
Button
的DataGridGolumn
获得焦点时,按 Enter 键应激活该命令。 (例如不需要按 Tab 键即可将焦点放在按钮上) - 使用 Enter 或 空格 单击按钮
我尝试了 DataGridTemplateColumn
和 CellTemplate
:
<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/