我需要以下行为:当我开始在窗口中输入时,会出现一个小文本框,其中已经输入了第一个字母,然后在我键入文本后按Enter键文本框应该消失,直到我再次键入该窗口。问题是当我设置Popup1.IsOpen = false
文本框仍然保留在窗口中时。
<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;
}
}
答案 0 :(得分:1)
你应该将e.Handled = true添加到Window KeyDown 1不会被提升并重新打开弹出窗口
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;
}
}
答案 1 :(得分:0)
始终调用Window_KeyDown_1()方法。你必须设置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;
}
}