c# – 在TextBox焦点上打开WPF Popup
发布时间:2020-12-15 06:50:29 所属栏目:百科 来源:网络整理
导读:当焦点在文本框上时,我想打开一个弹出窗口 这是我写的代码: Window x:Class="Testpopup.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Heig
|
当焦点在文本框上时,我想打开一个弹出窗口
这是我写的代码: <Window x:Class="Testpopup.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">
<Grid>
<StackPanel>
<TextBox x:Name="text" GotKeyboardFocus="text_GotKeyboardFocus" />
<Button Click="Button_Click" Content="but"/>
<Popup x:Name="popup" Width="100" Height="100" PlacementTarget="{Binding ElementName=text}"
StaysOpen="False">
<Grid>
<StackPanel>
<DatePicker />
<TextBox />
</StackPanel>
</Grid>
</Popup>
</StackPanel>
</Grid>
private void Button_Click(object sender,RoutedEventArgs e)
{
popup.IsOpen = true;
}
private void text_GotKeyboardFocus(object sender,KeyboardFocusChangedEventArgs e)
{
popup.IsOpen = true;
}
如果我点击按钮,一切正常 如果我删除StaysOpen =“False”,弹出窗口打开但从不关闭 我尝试将焦点放在弹出窗口之前打开它,但它不起作用 你有什么主意吗 ? 非常感谢, 解决方法
将以下绑定添加到您的Popup声明中:
StaysOpen="{Binding ElementName=text,Path=IsKeyboardFocused}"
这应该是诀窍. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
