gpt4 book ai didi

c# - 如何通过单击 wpf 中的按钮使用 TabIndex 导航光标?

转载 作者:太空宇宙 更新时间:2023-11-03 11:17:41 25 4
gpt4 key购买 nike

<Grid x:Name="LayoutRoot">
<Button x:Name="BtnNavigateTabIndex" Content="NavigateTabIndex" Margin="152,200,0,190" HorizontalAlignment="Left" Width="120"/>
<TextBox x:Name="TextBox_1" HorizontalAlignment="Left" Height="48" Margin="120,64,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="80"/>
<TextBox x:Name="TextBox_2" HorizontalAlignment="Left" Height="48" Margin="120,128,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="80"/>
<PasswordBox x:Name="PasswordBox_1" HorizontalAlignment="Left" Height="48" Margin="224,64,0,0" VerticalAlignment="Top" Width="72"/>
<PasswordBox x:Name="PasswordBox_2" HorizontalAlignment="Left" Height="48" Margin="224,128,0,0" VerticalAlignment="Top" Width="72"/>
</Grid>

设计将如下所示。

enter image description here

如果我单击按钮 (NavigateTabIndex),光标应导航到 TextBox 或 PasswordBox。对于 EX:如果您单击键盘中的 Tab 键,光标将导航。这就是我需要的场景。

最佳答案

一般来说,WPF 聚焦有很多陷阱......甚至还有一些错误。

您需要了解逻辑焦点和键盘焦点之间的区别。

使用 FocusScopes 时,您可以使用:

MoveFocus 带有 TraversalRequest(FocusNavigationDirection.Next)

将逻辑焦点更改为范围中的下一项。

使用 FocusScopes 的替代方法是跟踪 GotFocus/LostFocus 事件并自行管理。

更多链接:


好的,这里有一些示例代码供您使用。我冒昧地稍微重新设计了您对 Grid 的使用,以遵循更传统的布局实践。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication5
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void BtnNavigateTabIndex_Click(object sender, RoutedEventArgs e)
{
UIElement focussedelement = FocusManager.GetFocusedElement(grid1) as UIElement;

bool bmovedfocus = focussedelement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));

if (bmovedfocus)
{
UIElement withfocusnow = FocusManager.GetFocusedElement(grid1) as UIElement;

if (withfocusnow == focussedelement) // focus didn't change! because end of focus group..need to put it back to the start
{
TextBox_1.Focus();
}
}
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
TextBox_1.Focus();
}
}
}


<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication5"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<StackPanel>
<Grid x:Name="grid1" FocusManager.IsFocusScope="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBox Margin="10" Grid.Row="0" Grid.Column="0" x:Name="TextBox_1" HorizontalAlignment="Left" Height="48" TextWrapping="Wrap" VerticalAlignment="Top" Width="80"/>
<PasswordBox Margin="10" Grid.Row="0" Grid.Column="1" x:Name="PasswordBox_1" HorizontalAlignment="Left" Height="48" VerticalAlignment="Top" Width="72"/>
<TextBox Margin="10" Grid.Row="1" Grid.Column="0" x:Name="TextBox_2" HorizontalAlignment="Left" Height="48" TextWrapping="Wrap" VerticalAlignment="Top" Width="80"/>
<PasswordBox Margin="10" Grid.Row="1" Grid.Column="1" x:Name="PasswordBox_2" HorizontalAlignment="Left" Height="48" VerticalAlignment="Top" Width="72"/>
<Button FocusManager.IsFocusScope="True" Padding="20" HorizontalAlignment="Center" Grid.Row="2" Grid.ColumnSpan="2" x:Name="BtnNavigateTabIndex" Content="NavigateTabIndex" Width="120" Click="BtnNavigateTabIndex_Click" />
</Grid>
</StackPanel>
</Window>

关于c# - 如何通过单击 wpf 中的按钮使用 TabIndex 导航光标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12157905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com