gpt4 book ai didi

c# - 在 TextBox 焦点上打开 WPF 弹出窗口

转载 作者:可可西里 更新时间:2023-11-01 08:41:42 25 4
gpt4 key购买 nike

我想在焦点位于文本框上时打开一个弹出窗口这是我写的代码:

<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"弹出窗口打开但永远不会关闭

我尝试在打开弹出窗口之前将焦点设置在弹出窗口上,但效果不佳

你有什么想法吗?

非常感谢,尼达尔。

最佳答案

将以下绑定(bind)添加到您的 Popup 声明:

StaysOpen="{Binding ElementName=text,Path=IsKeyboardFocused}"

这应该可以解决问题。

关于c# - 在 TextBox 焦点上打开 WPF 弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3469706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com