我也没用WPF做过项目,我刚试了简单的小程序:
登录界面,刚开始鼠标定位在帐号输入框,按下回车键自动到密码输入框,再按下回车键自动定位到登录按钮
重点代码- <Grid>
- <TextBlock HorizontalAlignment="Left" Margin="23,47,0,0" TextWrapping="Wrap" Text="帐号" VerticalAlignment="Top" RenderTransformOrigin="0.436,-0.75"/>
- <TextBox Name="txtAccount" HorizontalAlignment="Left" Height="23" Margin="100,44,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="156" KeyDown="txtAccount_KeyDown"/>
- <TextBlock HorizontalAlignment="Left" Margin="23,93,0,0" TextWrapping="Wrap" Text="密码" VerticalAlignment="Top"/>
- <PasswordBox Name="txtPassword" HorizontalAlignment="Left" Height="23" Margin="100,90,0,0" VerticalAlignment="Top" Width="156" KeyDown="txtPassword_KeyDown"/>
- <Button Name="btnLogin" Content="登录" HorizontalAlignment="Left" Margin="100,154,0,0" VerticalAlignment="Top" Width="75" Click="btnLogin_Click"/>
- <Button Name="btnCancel" Content="取消" HorizontalAlignment="Left" Margin="181,154,0,0" VerticalAlignment="Top" Width="75"/>
- </Grid>
复制代码- private void winLogin_Loaded(object sender, RoutedEventArgs e)
- {
- txtAccount.Focus();
- }
- private void txtAccount_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Return)
- {
- txtPassword.Focus();
- }
- }
- private void txtPassword_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Return)
- {
- btnLogin.Focus();
- }
- }
复制代码 说明:
KeyDown事件中可以:if (e.Key == Key.Return)你将断点定在此,可以看到你点击的键盘事件.貌似Key.Return就是回车键吧.
|