簡體   English   中英

C# WPF 彈出窗口沒有出現

[英]C# WPF popup doesn't appear

我在編碼方面相當新,我正在努力處理一個不會在運行時出現的彈出窗口。

我正在嘗試制作一個自動完成/建議彈出列表,但我似乎無法讓它工作。

這是我的 XAML:

<Grid Grid.Row="2">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="130"/>
        <ColumnDefinition x:Name="editorInputColumn"/>
    </Grid.ColumnDefinitions>
        <TextBlock Grid.Column="0" Text="Ajouter (séparateur ';') :">
            <TextBlock.Foreground>
                <SolidColorBrush Color="{DynamicResource FontColor}"/>
            </TextBlock.Foreground>
        </TextBlock>     
    <Grid x:Name="popupEditorGrid" Grid.Column="1" Visibility="Visible">
        <Popup Placement="Top" Visibility="Visible" StaysOpen="True"  Panel.ZIndex="1000" x:Name="EditorPopup"  Grid.Column="1"  Width="{Binding Path=ActualWidth, ElementName=editorInputColumn}">
            <StackPanel x:Name="EditorPopupStackPanel">
                <StackPanel.Background>
                    <SolidColorBrush Color="{DynamicResource EllipseSecondary}"/>
                </StackPanel.Background>
                    <TextBlock Text="test"/><!--this is just an attempt at displaying something in the popup, but even this does not appear at runtime-->
            </StackPanel>
       </Popup>
   </Grid>
   <TextBox Grid.Column="1" KeyUp="editorAddInput_KeyUp" x:Name="editorAddInput" >
        <TextBox.BorderBrush>
            <SolidColorBrush Color="{DynamicResource BoutonMarge}"/>
        </TextBox.BorderBrush>
    </TextBox>
</Grid>

這是背后的代碼:

private void editorAddInput_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
    {
        string lastInput;

        List<string> inputList = editorAddInput.Text.ToUpper().Split(',', ';').ToList();

        if (inputList.Count != 0)
        {
            lastInput = inputList[inputList.Count - 1];
        }
        else
        {
            lastInput = editorAddInput.Text;
        }

        List<Editor> matchingEditorsList = new List<Editor>();

        EditorPopupStackPanel.Children.Clear();


        foreach(Editor editor in localEditorsList)//look up among all known names
        {
            if(editor.Name.StartsWith(lastInput))
            {
                matchingEditorsList.Add(editor);
            }
        }

        if(matchingEditorsList.Count!=0)
        {
            EditorPopup.Visibility = Visibility.Visible;

            foreach(Editor editor in matchingEditorsList)
            {
                EditorPopupStackPanel.Children.Add(new TextBlock() { Text = editor.Name });         
            }
            EditorPopup.StaysOpen = true;
            EditorPopup.IsOpen = true;

        }
        else
        {
            EditorPopup.Visibility = Visibility.Collapsed;
            EditorPopup.IsOpen = false;
        }
    }

輸入文本框應該能夠獲得多個名稱,以“;”分隔,因此我從輸入最后一個名稱開始。

有趣的是,彈出窗口在選中時顯示在Visual Studio的概念視圖中,但不在運行時出現。 我試過玩 z-index 沒有成功。 知道我搞砸了什么嗎?

將文本框放在 XAML 中的網格內

您缺少IsOpen屬性,該屬性應設置為true才能顯示彈出窗口。

暫無
暫無

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

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