簡體   English   中英

在TextBox焦點上打開WPF彈出窗口

[英]Open WPF Popup on TextBox focus

當焦點在文本框上時,我想打開一個彈出窗口這是我寫的代碼:

<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”彈出窗口打開但從不關閉

我嘗試在打開它之前將重點放在彈出窗口上,但它也不起作用

你有什么主意嗎 ?

非常感謝,Nidal。

將以下綁定添加到Popup聲明:

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

這應該可以解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM