我在WPF对话框中有一个默认按钮,我用于登录对话框,我将ok按钮设置为默认值,取消按钮取消。发生的事情是我必须在关闭对话框之前按两次Enter键。以下是一些代码片段,首先是xaml:
<Window x:Class="WPFClient.dlgLogin"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="User Login"
Height="220"
Width="372"
xmlns:igEditors="http://infragistics.com/Editors"
WindowStartupLocation="CenterScreen"
WindowStyle="ThreeDBorderWindow"
ShowInTaskbar="False"
FocusManager.FocusedElement="{Binding ElementName=xamtxtLogin}" ResizeMode="NoResize">
<Grid>
<Label Content="Login:" Height="28" HorizontalAlignment="Left" Margin="97,51,0,0" Name="lblLogin" VerticalAlignment="Top" />
<Label Content="Password:" Height="28" HorizontalAlignment="Left" Margin="78,78,0,0" Name="lblPassword" VerticalAlignment="Top" />
<igEditors:XamTextEditor HorizontalAlignment="Left" Margin="145,56,0,0" Name="xamtxtLogin" VerticalAlignment="Top" Width="156" />
<igEditors:XamTextEditor HorizontalAlignment="Left" Margin="145,84,0,0" Name="xamtxtPassword" VerticalAlignment="Top" Width="156" />
<Button Content="Ok" IsDefault="True" Height="23" HorizontalAlignment="Left" Margin="145,121,0,0" Name="bOk" VerticalAlignment="Top" Width="75" Click="bOk_Click" />
<Button Content="Cancel" IsCancel="True" Height="23" HorizontalAlignment="Left" Margin="226,121,0,0" Name="bCancel" VerticalAlignment="Top" Width="75" Click="bCancel_Click" />
</Grid>
</Window>
现在我的对话框代码:
public partial class dlgLogin : Window
{
public dlgLogin()
{
InitializeComponent();
}
private void bOk_Click( object sender, RoutedEventArgs e )
{
this.DialogResult = true;
this.Close();
}
private void bCancel_Click( object sender, RoutedEventArgs e )
{
this.DialogResult = false;
this.Close();
}
}
最后我的代码在使用对话框的窗口中:
private void Login()
{
dlgLogin LoginDialog = new dlgLogin();
LoginDialog.Owner = this;
LoginDialog.ShowDialog();
if ( LoginDialog.DialogResult.HasValue && LoginDialog.DialogResult.Value )
{
MessageBox.Show( "Ok clicked." );
this.LoginActive = LoginDialog.DialogResult.Value;
}
else
{
MessageBox.Show( "Cancel clicked." );
this.LoginActive = false;
}
}
这是我的第一个WPF应用程序,我不确定我是否遗漏了一些东西。我的理解是,当你有一个默认的按钮设置时,只需点击输入即可触发按钮的事件,但出于某种原因,我总是要点击它两次才能工作。
感谢。