Dork
Dork

Reputation: 1886

Popup's text box is not dissapearing after popup.isOpen = false

I need following behavior: When I start typing in window little textbox appears with the first letter typed already in it, then after I typed text and press enter textbox should dissapear until I will type in that window again. The problem is when I set Popup1.IsOpen = false text box still remain in a window.

<Window x:Class="Beta.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" KeyDown="Window_KeyDown_1">  
  <Grid> 
    <Popup Name="Popup1" IsEnabled="True" IsOpen="False" VerticalOffset="-200" HorizontalOffset="50">
       <TextBox Name="tbx" Width="50" KeyDown="tbx_KeyDown" />
     </Popup>
  </Grid>
</Window>

string temp;

private void Window_KeyDown_1(object sender, KeyEventArgs e)
    {
        Popup1.IsOpen = true;
        tbx.Focus();
    }

private void tbx_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            Popup1.IsOpen = false;
            temp = tbx.Text;
            tbx.Text = null;

        }
    }

Upvotes: 1

Views: 240

Answers (2)

rhe1980
rhe1980

Reputation: 1577

The Window_KeyDown_1() method is called always. You have to set e.Handled=true

  private void tbx_KeyDown(object sender, KeyEventArgs e)
  {
     if (e.Key == Key.Enter)
     {
        Popup1.IsOpen = false;
        temp = tbx.Text;
        tbx.Text = null;
        e.Handled = true;
     }
  }

Upvotes: 0

user1064519
user1064519

Reputation: 2190

You sholud add e.Handled =true, so the Window_KeyDown_1 wont be raised and reopen the popup

   private void tbx_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            Popup1.IsOpen = false;
            temp = tbx.Text;
            tbx.Text = null;
            e.Handled = true;

        }
    }

Upvotes: 1

Related Questions