0

I'm using xceed wpf Toolkit. In that I'm using ChildWindow. I need to close the opened Child window on escape key press. Here is the code

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

Can you help me ??

Krishna Thota
  • 6,646
  • 14
  • 54
  • 79

2 Answers2

1

Use the "IsCancel" Property on your Button.

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

Same for IsDefault (Enter Key)

0

If you use version 2.0.0 or higher you should put ChildWindow in WindowContainer and use PreviewKeyDown event.

XAML:

<xctk:WindowContainer>
    <xctk:ChildWindow x:Name="ChildVendorsEdit" IsModal="True" WindowStartupLocation="Center" Caption="Edit"                          
                  PreviewKeyDown="ChildVendorsEdit_PreviewKeyDown" >                
    </xctk:ChildWindow>
</xctk:WindowContainer>

Code-behind:

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;
    }
}

If you use version below 2.0.0 you should use PreviewKeyDown event:

XAML:

<xctk:ChildWindow x:Name="ChildVendorsEdit" IsModal="True" WindowStartupLocation="Center" Caption="Edit"                          
                  PreviewKeyDown="ChildVendorsEdit_PreviewKeyDown" >            
</xctk:ChildWindow>

Code-behind:

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;
    }
}

To close ChildWindow in PreviewKeyDown event handler you have two options:

  • you can set WindowState to Closed,
  • or you can invoke Close method.
kmatyaszek
  • 19,016
  • 9
  • 60
  • 65