ItemsControl 中的 WPF TabStop / TabIndex

发布于 2024-08-19 07:39:16 字数 2026 浏览 4 评论 0原文

我正在动态添加 WPF ComboBox-es,并且希望能够使用“TAB”键选择添加的组合框。 (动态生成TabIndex什么的)

实际情况是:

替代文字

我想要的:

替代文本

你会怎么做?

这是代码:

<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'm dynamically adding WPF ComboBox-es and I want to be able to select the added ComboBoxes with the 'TAB' key. (Dynamically generate TabIndex or something)

As it actually is:

alt text

What I want:

alt text

How would you do that?

This is the code:

<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>

Thanks in advance !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

囚我心虐我身 2024-08-26 07:39:16

这是一个小例子。我在 Silverlight 中完成了此操作,因此我想您必须修改它才能在 WPF 中工作。它并不完整,但它是一个起点。您可能希望使用控件上的 Tag 属性来确定哪些控件需要修复选项卡索引。

XAML:

<UserControl x:Class="SilverlightApplication6.MainPage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             d:DesignWidth="640"
             d:DesignHeight="480">
    <Grid x:Name="LayoutRoot">

        <StackPanel>
            <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">
                                <TextBox.Text>
                                    <Binding Path="Wert"
                                             Mode="TwoWay" />
                                </TextBox.Text>
                            </TextBox>

                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
            <Button Content="Fix Tab indexes" Click="Button_Click"
                    TabIndex="999"
                    ></Button>
        </StackPanel>
    </Grid>
</UserControl>

和 C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace SilverlightApplication6
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            this.DataContext = new List<int>() { 1, 2, 3, 4, 5 };


        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var controls = new List<Control>();
            GetChildrenOfSpecificType<Control>(this, ref controls, false);

            var index = 1;
            controls.ForEach(control =>
                {
                    if (control is ComboBox || control is TextBox)
                    {
                        control.TabIndex = index;
                        index++;
                    }
                });
        }

        private static void GetChildrenOfSpecificType<T>(DependencyObject parent, ref List<T> resultList, bool getSingle) where T : class
        {
            if (parent == null)
                return;
            else
            {
                int cnt = VisualTreeHelper.GetChildrenCount(parent);

                if (cnt > 0)
                {
                    for (int i = 0; i < cnt; i++)
                    {
                        var d = VisualTreeHelper.GetChild(parent, i);
                        if (d is T)
                        {
                            resultList.Add(d as T);
                            if (getSingle)
                                return;
                        }

                        GetChildrenOfSpecificType<T>(d, ref resultList, getSingle);
                    }
                }

            }
        }
    }
}

Here is a small example. I did this in Silverlight, so I guess you will have to modify it to work in WPF. It is not complete, but it is a starting point. You might want to use the Tag property on your controls to identify which of them need tab index fixing.

XAML:

<UserControl x:Class="SilverlightApplication6.MainPage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             d:DesignWidth="640"
             d:DesignHeight="480">
    <Grid x:Name="LayoutRoot">

        <StackPanel>
            <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">
                                <TextBox.Text>
                                    <Binding Path="Wert"
                                             Mode="TwoWay" />
                                </TextBox.Text>
                            </TextBox>

                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
            <Button Content="Fix Tab indexes" Click="Button_Click"
                    TabIndex="999"
                    ></Button>
        </StackPanel>
    </Grid>
</UserControl>

And C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace SilverlightApplication6
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            this.DataContext = new List<int>() { 1, 2, 3, 4, 5 };


        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var controls = new List<Control>();
            GetChildrenOfSpecificType<Control>(this, ref controls, false);

            var index = 1;
            controls.ForEach(control =>
                {
                    if (control is ComboBox || control is TextBox)
                    {
                        control.TabIndex = index;
                        index++;
                    }
                });
        }

        private static void GetChildrenOfSpecificType<T>(DependencyObject parent, ref List<T> resultList, bool getSingle) where T : class
        {
            if (parent == null)
                return;
            else
            {
                int cnt = VisualTreeHelper.GetChildrenCount(parent);

                if (cnt > 0)
                {
                    for (int i = 0; i < cnt; i++)
                    {
                        var d = VisualTreeHelper.GetChild(parent, i);
                        if (d is T)
                        {
                            resultList.Add(d as T);
                            if (getSingle)
                                return;
                        }

                        GetChildrenOfSpecificType<T>(d, ref resultList, getSingle);
                    }
                }

            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文