ItemsControl中的WPF TabStop / TabIndex

编程入门 行业动态 更新时间:2024-10-28 07:31:55
本文介绍了ItemsControl中的WPF TabStop / TabIndex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在动态添加WPF ComboBox-es,并且希望能够通过键 选择添加的ComboBox。 (动态生成TabIndex或其他内容)

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)

实际上是:

我想要的内容:

您将如何处理?

这是代码:

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

预先感谢!

推荐答案

这里是一个小例子。我是在Silverlight中完成此操作的,所以我想您必须对其进行修改才能在WPF中使用。它并不完整,但这是一个起点。您可能想在控件上使用Tag属性来标识其中哪些需要固定选项卡索引。

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="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/winfx/2006/xaml" xmlns:d="schemas.microsoft/expression/blend/2008" xmlns:mc="schemas.openxmlformats/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); } } } } } }

更多推荐

ItemsControl中的WPF TabStop / TabIndex

本文发布于:2023-11-12 03:03:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580322.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:WPF   ItemsControl   TabIndex   TabStop

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!