C#WPF弹出窗口没有出现

时间:2018-08-27 10:14:44

标签: c# wpf popup

我在编码方面还很陌生,我正在努力寻找一个在运行时不会出现的弹出窗口。

我正在尝试制作一个自动完成/建议弹出列表,但我似乎无法使其工作。

这是我的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的Conceptor视图中,但在运行时却没有出现。我尝试使用z-index失败。对我搞砸了有什么想法吗?

2 个答案:

答案 0 :(得分:0)

在XAML中将文本框放入网格中

答案 1 :(得分:0)

您缺少 IsOpen 属性,该属性应设置为 true 才能显示弹出窗口。