如何使用数据模板在 ItemsControl 中获取逐行 Tab 键顺序?

发布于 2024-08-18 23:40:13 字数 2450 浏览 2 评论 0原文

我希望选项卡向前浏览一行中的每个项目,并且针对每一行。但实际上它会逐一列地遍历所有项目!

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

祁梦 2024-08-25 23:40:13

您已在 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 的同级。

像这样:

                        <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"/>
                                        <ComboBox Grid.Column="1" ItemsSource="{Binding Source={StaticResource SomeOtherItems}}" IsSynchronizedWithCurrentItem="False" SelectedItem="{Binding Path=SomeOtherValue, Mode=TwoWay}" DisplayMemberPath="Value"/>
                                        <TextBox HorizontalContentAlignment="Stretch" Grid.Column="2" LostKeyboardFocus="TextBox_FormatAfterLostFocus">
                                            <TextBox.Text>
                                                <Binding Path="Wert" Mode="TwoWay" />
                                            </TextBox.Text>
                                        </TextBox>
                                    </Grid>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>

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:

                        <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"/>
                                        <ComboBox Grid.Column="1" ItemsSource="{Binding Source={StaticResource SomeOtherItems}}" IsSynchronizedWithCurrentItem="False" SelectedItem="{Binding Path=SomeOtherValue, Mode=TwoWay}" DisplayMemberPath="Value"/>
                                        <TextBox HorizontalContentAlignment="Stretch" Grid.Column="2" LostKeyboardFocus="TextBox_FormatAfterLostFocus">
                                            <TextBox.Text>
                                                <Binding Path="Wert" Mode="TwoWay" />
                                            </TextBox.Text>
                                        </TextBox>
                                    </Grid>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文