如何使用数据模板在 ItemsControl 中获取逐行 Tab 键顺序?
我希望选项卡向前浏览一行中的每个项目,并且针对每一行。但实际上它会逐一列地遍历所有项目!
DataTemplate 中有 2 个组合框(假设为 cb1 和 cb1)和一个文本框 (tb)。实际的 Tab 键顺序如下:
Row0.cb1, Row1.cb1 ... 行0.cb2, 行1.cb2 ... 行0.tb, Row1.tb ...
但我想要的是:
Row0.cb1, 行0.cb2, 行0.tb, 行1.cb1, 行1.cb2, Row1.tb ...
<ItemsControl ItemsSource="{Binding}" Name="myItemsControl">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ComboBox Grid.Column="0" ItemsSource="{Binding Source={StaticResource SomeItems}}" IsSynchronizedWithCurrentItem="False" SelectedItem="{Binding Path=SomeValue, Mode=TwoWay}" DisplayMemberPath="Name" TabIndex="20"/>
<ComboBox Grid.Column="1" ItemsSource="{Binding Source={StaticResource SomeOtherItems}}" IsSynchronizedWithCurrentItem="False" SelectedItem="{Binding Path=SomeOtherValue, Mode=TwoWay}" DisplayMemberPath="Value" TabIndex="21"/>
<TextBox HorizontalContentAlignment="Stretch" Grid.Column="2" TabIndex="22" LostKeyboardFocus="TextBox_FormatAfterLostFocus">
<TextBox.Text>
<Binding Path="Wert" Mode="TwoWay" />
</TextBox.Text>
</TextBox>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I want the Tab go forward through each item in a row, and this for each row. But actually it goes through all items in a column, column after column!
In the DataTemplate are 2 Comboboxes (let's say cb1 and cb1) and one TextBox (tb). The actual tab order is the following:
Row0.cb1,
Row1.cb1
...
Row0.cb2,
Row1.cb2
...
Row0.tb,
Row1.tb
...
But what i want is:
Row0.cb1,
Row0.cb2,
Row0.tb,
Row1.cb1,
Row1.cb2,
Row1.tb
...
<ItemsControl ItemsSource="{Binding}" Name="myItemsControl">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ComboBox Grid.Column="0" ItemsSource="{Binding Source={StaticResource SomeItems}}" IsSynchronizedWithCurrentItem="False" SelectedItem="{Binding Path=SomeValue, Mode=TwoWay}" DisplayMemberPath="Name" TabIndex="20"/>
<ComboBox Grid.Column="1" ItemsSource="{Binding Source={StaticResource SomeOtherItems}}" IsSynchronizedWithCurrentItem="False" SelectedItem="{Binding Path=SomeOtherValue, Mode=TwoWay}" DisplayMemberPath="Value" TabIndex="21"/>
<TextBox HorizontalContentAlignment="Stretch" Grid.Column="2" TabIndex="22" LostKeyboardFocus="TextBox_FormatAfterLostFocus">
<TextBox.Text>
<Binding Path="Wert" Mode="TwoWay" />
</TextBox.Text>
</TextBox>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已在 ItemsControl 中设置 TabIndex 值。 WPF 所做的是为每一行提供相同的 TabIndices,这意味着:
row1.cb1.TabIndex =20 |row1.cb2.TabIndex = 21| row1.tb.TabIndex = 22
row2.cb1.TabIndex =20 |row2.cb2.TabIndex = 21| row2.tb.TabIndex = 22
因为第二行的 20 低于第一行的 21 第二个组合框 wpf 将首先循环遍历行,然后再循环遍历列。
尝试忽略手动设置的 TabIndex 值!这样,它使用 WPF 自动选项卡首先循环遍历子级,然后遍历 XAML 的同级。
像这样:
you have set TabIndex Values in an ItemsControl. What WPF does is give every row the same TabIndices this means:
row1.cb1.TabIndex =20 |row1.cb2.TabIndex = 21| row1.tb.TabIndex = 22
row2.cb1.TabIndex =20 |row2.cb2.TabIndex = 21| row2.tb.TabIndex = 22
as the 20 of the second row is lower than 21 of first rows second combobox wpf will first cycle through the rows before cycling through the columns.
try leaving out the manually set TabIndex Values! That way it's using WPFs automated tabbing to cycle first through children and then through siblings of the XAML.
like so: