微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 在TextBox焦点上打开WPF Popup

当焦点在文本框上时,我想打开一个弹出窗口
这是我写的代码
<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}"

这应该是诀窍.

原文地址:https://www.jb51.cc/csharp/94441.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐