WPF - 如何为 Itemscontrol 中的特定项目设置 Tab 键顺序
WPF - How to set a Tab Order for specific items inside an Itemscontrol
我有一个动态创建文本框的 ItemsControl。当我切换时,我只希望它专注于一个特定的文本框。
切换时,焦点应跳到每个 Coin_Qty_TxtBx
我一直在尝试使用 KeyboardNavigation 选项的不同选项,但无法使其正常工作。我还尝试手动将文本框上的 TabIndex 绑定到整数列表。
<ItemsControl x:Name="Coins_LB" BorderThickness="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" KeyboardNavigation.TabNavigation="Continue">
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel IsItemsHost="True" Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Grid.Column="0">
<Button x:Name="CoinPicture_Btn" BorderThickness="0" Background="white" Style="{StaticResource AddRemoveSave_Style}" VerticalAlignment="Top" Height="60" Width="60" HorizontalAlignment="Center" Margin="0,0,0,3" Click="CashPicture_Btn_Click" IsTabStop="False">
<Image Source="{Binding PicturePath}" RenderOptions.BitmapScalingMode="Fant" Margin="-15, -20, -15, -30" />
</Button>
<Label x:Name="Coin_Name_Lbl" Content="{Binding Name}" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Margin="0,1,0,0" VerticalAlignment="Top" Width="72" FontWeight="Bold" Grid.Column="0"/>
<TextBox x:Name="Coin_Qty_TxtBx" Text="{Binding Count, Mode=TwoWay}" Margin="5,2" IsTabStop="True" HorizontalContentAlignment="Center" LostFocus="Cash_Qty_LostFocus" GotKeyboardFocus="Cash_Qty_GotFocus"/>
<TextBox x:Name="Coin_ItemTotal_TxtBx" Text="{Binding Total, StringFormat='c'}" Margin="5,2" HorizontalContentAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
现在它会专注于第一个 Coin_Qty_TxtBx,但从那里按 Tab 键后,焦点就会完全离开 ItemsControl。
您只需在这些控件上设置 IsTabStop="False"
,您不想使用 Tab 键。
例如:
<TextBox x:Name="Coin_ItemTotal_TxtBx" IsTabStop="False"... />
我有一个动态创建文本框的 ItemsControl。当我切换时,我只希望它专注于一个特定的文本框。
切换时,焦点应跳到每个 Coin_Qty_TxtBx
我一直在尝试使用 KeyboardNavigation 选项的不同选项,但无法使其正常工作。我还尝试手动将文本框上的 TabIndex 绑定到整数列表。
<ItemsControl x:Name="Coins_LB" BorderThickness="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" KeyboardNavigation.TabNavigation="Continue">
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel IsItemsHost="True" Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Grid.Column="0">
<Button x:Name="CoinPicture_Btn" BorderThickness="0" Background="white" Style="{StaticResource AddRemoveSave_Style}" VerticalAlignment="Top" Height="60" Width="60" HorizontalAlignment="Center" Margin="0,0,0,3" Click="CashPicture_Btn_Click" IsTabStop="False">
<Image Source="{Binding PicturePath}" RenderOptions.BitmapScalingMode="Fant" Margin="-15, -20, -15, -30" />
</Button>
<Label x:Name="Coin_Name_Lbl" Content="{Binding Name}" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Margin="0,1,0,0" VerticalAlignment="Top" Width="72" FontWeight="Bold" Grid.Column="0"/>
<TextBox x:Name="Coin_Qty_TxtBx" Text="{Binding Count, Mode=TwoWay}" Margin="5,2" IsTabStop="True" HorizontalContentAlignment="Center" LostFocus="Cash_Qty_LostFocus" GotKeyboardFocus="Cash_Qty_GotFocus"/>
<TextBox x:Name="Coin_ItemTotal_TxtBx" Text="{Binding Total, StringFormat='c'}" Margin="5,2" HorizontalContentAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
现在它会专注于第一个 Coin_Qty_TxtBx,但从那里按 Tab 键后,焦点就会完全离开 ItemsControl。
您只需在这些控件上设置 IsTabStop="False"
,您不想使用 Tab 键。
例如:
<TextBox x:Name="Coin_ItemTotal_TxtBx" IsTabStop="False"... />