在WPF数据网格的列之间禁用Tabstop [英] Disable tabstop between columns in a WPF datagrid
问题描述
我有一个带有多列的WPF Toolkit数据网格。我正在尝试一种行为,您可以使用Tab键将其切换到网格中,然后使用单个标签再次跳出。例如。我不想一次又一次地浏览网格的所有列或单元格。
有没有简单的解决方案,我尝试过设置TabNavigation为一次,以及禁用TabStop(在下面的代码中未显示)并将列上的TabNavigation设置为无,但没有成功。
是否缺少某些内容或我需要处理代码中的Tab键吗?
< my:DataGrid Name = datagrid
AutoGenerateColumns = False IsReadOnly = True
CanUserAddRows = False CanUserDeleteRows = False
Background = White
KeyboardNavigation.TabNavigation = Once>
< my:DataGrid.Columns>
< my:DataGridTextColumn x:Name = ID Header = ID Width = 1 *>< / my:DataGridTextColumn>
< my:DataGridTextColumn x:Name = Ticker Header = Ticker Width = 1 * KeyboardNavigation.TabNavigation = None>< / my:DataGridTextColumn>
< my:DataGridTextColumn x:Name = OfficialName Header = Name Width = 3 * KeyboardNavigation.TabNavigation = None>< / my:DataGridTextColumn>
< / my:DataGrid.Columns>
< / my:DataGrid>
有趣的是,直接在DataGridTextColumn的上设置KeyboardNavigation不行可行的替代方法是设置DataGridCell样式。
< toolkit:DataGrid.CellStyle>
< Style TargetType = {x:Type工具箱:DataGridCell}>
< Setter Property = KeyboardNavigation.IsTabStop
Value = False />
< Style.Triggers>
< Trigger Property = IsSelected
Value = True>
< Setter Property = KeyboardNavigation.IsTabStop
Value = True />
< / Trigger>
< /Style.Triggers>
< / Style>
< / toolkit:DataGrid.CellStyle>
将其附加到DataGrid将确保单元格只是一个TabStop(如果已被选中)。但是,如果您选择的是完整行,并且未在DataGrid上设置SelectionUnit = Cell,它将仍然在当前选定行的每一列中循环。
相反,我们可以在DataGrid中创建多个CellStyles作为资源:
< toolkit:DataGrid.Resources>
< Style x:Key = SelectableCellStyle
TargetType = {x:Type toolkit:DataGridCell}>
< Setter Property = KeyboardNavigation.IsTabStop
Value = False />
< Style.Triggers>
< Trigger Property = IsSelected
Value = True>
< Setter Property = KeyboardNavigation.IsTabStop
Value = True />
< / Trigger>
< /Style.Triggers>
< / Style>
< Style TargetType = {x:Type toolkit:DataGridCell}>
< Setter Property = KeyboardNavigation.IsTabStop
Value = False />
< / Style>
< / toolkit:DataGrid.Resources>
现在,我们已经默认将一种样式应用于所有DataGridCells并关闭TabStop和一种键控样式当选择单元格(或整个行)时,允许选择。仅将这种样式应用于单个列将为我们提供相同的单选项卡效果,同时允许选择整行及其所有列。
< my:DataGridTextColumn x:Name = ID Header = ID Width = 1 * CellStyle = {StaticResource SelectableCellStyle} />
如果未选择任何内容,这也会停止跳入DataGrid,根据您使用的情况,这是首选还是不首选。 p>
I have a WPF Toolkit datagrid with mulitple columns. I am trying to get a behaviour where you can tab into the grid using tab, then tab out again using a single tab. E.g. I do not want to tab through all the columns or cells of the grid, just once in, and once out.
Is there a simple solution, I have tried setting the TabNavigation to Once, along with disabling TabStop (not shown in code below) and setting TabNavigation on the columns to None, but without success.
Is there something I am missing or do I need to handle the Tab-key in code?
<my:DataGrid Name="datagrid"
AutoGenerateColumns="False" IsReadOnly="True"
CanUserAddRows="False" CanUserDeleteRows="False"
Background="White"
KeyboardNavigation.TabNavigation="Once">
<my:DataGrid.Columns>
<my:DataGridTextColumn x:Name="ID" Header="ID" Width="1*" ></my:DataGridTextColumn>
<my:DataGridTextColumn x:Name="Ticker" Header="Ticker" Width="1*" KeyboardNavigation.TabNavigation="None"></my:DataGridTextColumn>
<my:DataGridTextColumn x:Name="OfficialName" Header="Name" Width="3*" KeyboardNavigation.TabNavigation="None"></my:DataGridTextColumn>
</my:DataGrid.Columns>
</my:DataGrid>
It's interesting that setting the KeyboardNavigation directly on the DataGridTextColumn's doesn't work. An alternative that should work is to set up a DataGridCell style.
<toolkit:DataGrid.CellStyle>
<Style TargetType="{x:Type toolkit:DataGridCell}">
<Setter Property="KeyboardNavigation.IsTabStop"
Value="False" />
<Style.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="KeyboardNavigation.IsTabStop"
Value="True" />
</Trigger>
</Style.Triggers>
</Style>
</toolkit:DataGrid.CellStyle>
Attaching this to the DataGrid will ensure that a cell is only a TabStop if it is already selected. However, if you are selecting full rows and don't have SelectionUnit="Cell" set on the DataGrid, it will still cycle through each column of the currently selected row.
Instead, we can create multiple CellStyles as resources within the DataGrid:
<toolkit:DataGrid.Resources>
<Style x:Key="SelectableCellStyle"
TargetType="{x:Type toolkit:DataGridCell}">
<Setter Property="KeyboardNavigation.IsTabStop"
Value="False" />
<Style.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="KeyboardNavigation.IsTabStop"
Value="True" />
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type toolkit:DataGridCell}">
<Setter Property="KeyboardNavigation.IsTabStop"
Value="False" />
</Style>
</toolkit:DataGrid.Resources>
Now we have a style being applied to all DataGridCells by default and turning off TabStop, and a keyed style that allows selection when the Cell (or whole Row) is selected. Applying this style to only a single column will give us the same single-tab-in effect while allowing the whole row and all of it's columns to be selected.
<my:DataGridTextColumn x:Name="ID" Header="ID" Width="1*" CellStyle={StaticResource SelectableCellStyle}"/>
This does also stop tabbing into the DataGrid if nothing is selected, which may be preferred or not depending on the situation you are using it in.
这篇关于在WPF数据网格的列之间禁用Tabstop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!