2013-10-20 68 views
0

我正在使用xceed wpf工具包。 因爲我在使用ChildWindow。我需要在退出鍵按下時關閉打開的子窗口。這裏是代碼關閉xceed在逃生按鈕上的子窗口點擊

<xctk:ChildWindow x:Name="ChildVendorsEdit" IsModal="True" WindowStartupLocation="Center" Caption="Edit" > 
//My Content Here 
</xctk:ChildWindow> 

你能幫我嗎?

回答

0

如果您使用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你有兩種選擇:

  • 可以設置WindowStateClosed
  • 或者你可以調用Close方法。
1

在按鈕上使用「IsCancel」屬性。

<Button Content="Discard" Click="ButtonDiscard_OnClick" IsCancel="True"></Button> 

同爲ISDEFAULT(回車鍵)

相關問題