如果您使用 2.0.0 或更高版本,则应将 ChildWindow 放入 WindowContainer 并使用 PreviewKeyDown 事件。
XAML:
<xctk:WindowContainer>
<xctk:ChildWindow x:Name="ChildVendorsEdit" IsModal="True" WindowStartupLocation="Center" Caption="Edit"
PreviewKeyDown="ChildVendorsEdit_PreviewKeyDown" >
</xctk:ChildWindow>
</xctk:WindowContainer>
代码隐藏:
private void ChildVendorsEdit_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
(sender as Xceed.Wpf.Toolkit.ChildWindow).WindowState = Xceed.Wpf.Toolkit.WindowState.Closed;
}
}
如果你使用低于 2.0.0 的版本,你应该使用 PreviewKeyDown 事件:
XAML:
<xctk:ChildWindow x:Name="ChildVendorsEdit" IsModal="True" WindowStartupLocation="Center" Caption="Edit"
PreviewKeyDown="ChildVendorsEdit_PreviewKeyDown" >
</xctk:ChildWindow>
代码隐藏:
private void ChildVendorsEdit_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
(sender as Xceed.Wpf.Toolkit.ChildWindow).WindowState = Xceed.Wpf.Toolkit.WindowState.Closed;
}
}
要在PreviewKeyDown 事件处理程序中关闭ChildWindow,您有两种选择:
- 可以将
WindowState设置为Closed,
- 或者你可以调用
Close方法。