Lea*_*ner 8 c# wpf xaml wpf-controls wpfdatagrid
我有一个WPF window
作为模态对话框打开.
在对话框中,我有OK
&Cancel
按钮,其属性分别设置为IsDefault
&.这两个按钮都有关闭对话框的事件处理程序.IsCancel
True
Click
这是相关的XAML:
<StackPanel Orientation="Horizontal" Grid.Row="1" Height="45" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="190">
<Button Content="OK"
Height="25" Margin="10,10,10,10" Width="75" Name="btnOK" TabIndex="1600" IsDefault="True" Click="btnOK_Click"
VerticalContentAlignment="Center" HorizontalContentAlignment="Center" />
<Button Content="Cancel"
Height="25" Margin="10,10,10,10" Width="75" Name="btnCancel" TabIndex="1700" IsCancel="True"
VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Click="btnCancel_Click" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
这是后面的代码:
private void btnOK_Click(object sender, RoutedEventArgs e)
{
// My some business logic is here
this.Close();
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
Run Code Online (Sandbox Code Playgroud)
当我按下Esc
键盘上的Cancel
按钮时(即使焦点不在按钮上),对话框也会按预期关闭.但是,当按下按钮时按下Enter
按键时OK
没有任何反应.
我DataGrid
在对话框上有一个.我想在数据网格中选择任何行并按Enter键时关闭对话框.
怎么让这件事发生?
一些附加信息:对话框中有一个文本框.它有事件的事件处理程序Keyboard.PreviewKeyDown
.当我在文本框中并按Enter键时,不应关闭对话框.但是我可以删除这个处理程序.重要的是要解决上面的问题.
private void tbxSearchString_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
this.Search(); // Does some searching
}
}
Run Code Online (Sandbox Code Playgroud)
你的代码对我来说很好.按Enter键时关闭对话框.你可以写e.Handled = true; 在tbxSearchString_PreviewKeyDown事件中搜索功能之后的行.所以它不会关闭对话框.
<Grid>
<TextBox Name="tbxSearchString" HorizontalAlignment="Left" Width="100" Height="30" Grid.Row="0" PreviewKeyDown="tt_PreviewKeyDown"></TextBox>
<StackPanel Orientation="Horizontal" Grid.Row="1" Height="45" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="190">
<Button Content="OK"
Height="25" Margin="10,10,10,10" Width="75" Name="btnOK" TabIndex="1600" IsDefault="True" Click="btnOK_Click"
VerticalContentAlignment="Center" HorizontalContentAlignment="Center" />
<Button Content="Cancel"
Height="25" Margin="10,10,10,10" Width="75" Name="btnCancel" TabIndex="1700" IsCancel="True"
VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Click="btnCancel_Click" />
</StackPanel>
</Grid>
Run Code Online (Sandbox Code Playgroud)
private void btnOK_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void tbxSearchString_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
this.Search();
e.Handled = true;
}
}
Run Code Online (Sandbox Code Playgroud)
没有内置的方法来关闭wpf中的对话框窗口.你要做的是将DialogResult设置为默认按钮.所以你需要的是以下内容:
XAML
<Button Content="OK"
Height="25" Margin="10,10,10,10" Width="75" Name="btnOK" TabIndex="1600" IsDefault="True" Click="btnOK_Click"
VerticalContentAlignment="Center" HorizontalContentAlignment="Center" />
Run Code Online (Sandbox Code Playgroud)
代码隐藏:
private void btnOK_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
Run Code Online (Sandbox Code Playgroud)
你不应该自己打电话Close()
或处理PreviewKeyDown
.
要做到这一点,正确的方法是有确定/取消按钮和使用Button.IsDefault
,Button.IsCancel
以及Window.DialogResult
.如果在文本框中Window
未按"输入"按下,则按键将传播到该按钮,将按下默认按钮.
MyForm.xaml:
<Button x:Name="btnOk" Content="Ok" Click="btnOk_Click" IsDefault="True"/>
<Button x:Name="btnCancel" Content="Cancel" Click="btnCancel_Click" IsCancel="True"/>
Run Code Online (Sandbox Code Playgroud)
MyForm.xaml.cs:
private void btnOk_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
}
Run Code Online (Sandbox Code Playgroud)
现在点击表单中任何文本框的输入或转义将关闭表单(具有正确的结果)