Search code examples
c#wpfuser-controlscontrolskeyup

How to know control is handled or no inside user-control key-up event


I have some UIElements in my UserControl. Some of them might be a multiline textbox some of them might not be. For example:

 <Usercontrol PreviewKeyDown="Wizard_PreviewKeyDown">
    <StackPanel>
      <TextBox/>
      <TextBox AcceptsReturn="True"/>
    </StackPanel>
 <Usercontrol>

Code behind of this control:

 private void Wizard_PreviewKeyDown(object sender, KeyEventArgs e)
 {
     Key key = (e.Key == Key.System ? e.SystemKey : e.Key);
     if (key == Key.Enter)
     {
         UIElement uiElement = (e.OriginalSource as UIElement);
         GoToNext(); //if uiElement is already handled I should not call this method
     }
 }

I should not call my GoToNext() method if uiElement is alredy handled. In this case if I select my first textbox and click Enter it should go to next because this textbox is not a multiline. If I select my second textbox it should not go next because it is a multiline. How can I know is UIElement Handled or not? Can someone help me with this problem?

Thanks!


Solution

  • I used KeyDown="Wizard_KeyDown" instead of PreviewKeyDown="Wizard_PreviewKeyDown" and it's working as I wanted.