none
Popup不自动关闭问题 RRS feed

  • 问题

  • <Popup Placement="Bottom" StaysOpen="False" x:Name="popup" Opened="popup_Opened" Focusable="True" Closed="popup_Closed_1"
                           IsOpen="{Binding Path=IsDropDownOpen,RelativeSource={RelativeSource  Mode=FindAncestor,AncestorType={x:Type local:YQAddress}}}">

    Popup属性如上。

    现在我想问的是,Combobox的下拉框都可以在点击窗口关闭按钮时自动关闭,为什么自己定义的Popup不能自动关闭,而且我拖动窗口,Popup也不跟着走。。

    2017年7月6日 6:08

全部回复

  • Hi,

    >>Combobox的下拉框都可以在点击窗口关闭按钮时自动关闭,为什么自己定义的Popup不能自动关闭。

    当Popup处于打开状态时,必须点击两次关闭按钮,才能关闭窗口,第一次按钮的会导致PopUP关闭(因为焦点在popup上),第二次才能关闭窗口。如果交代你不在popup上,就可以一次关闭,这是我这里的情况。

    >>而且我拖动窗口,Popup也不跟着走。。

    这个控件设计的时候就是这样的。

    尝试重写windows的OnLocationChanged方法来处理:

       protected override void OnLocationChanged(EventArgs e)
            {
                popup.HorizontalOffset += 1;
                popup.HorizontalOffset -= 1;
                base.OnLocationChanged(e);
            }

    或者尝试:使用下面的第三方XAML扩展(Markup Programming 

           xmlns:p=http://markupprogramming.codeplex.com/markup/programming
       <StackPanel>
            <Button
                x:Name="Btn1"
                Width="100"
                Height="40"
                Click="Button_Click"
                Content="click" />
            <Popup
                x:Name="popup"
                AllowsTransparency="True"
                IsOpen="False"
                KeyDown="popup_KeyDown"
                Placement="Bottom"
                PlacementTarget="{Binding ElementName=Btn1}"
                PopupAnimation="Slide"
                StaysOpen="True">
                <StackPanel
                    x:Name="stackPanel"
                    Width="{Binding ElementName=Btn1, Path=Width}"
                    Background="{Binding ElementName=Btn1, Path=Background}"
                    Focusable="True">
                    <Button
                        x:Name="mybtn"
                        Height="40"
                        Content="button" />
                    <Rectangle
                        x:Name="MyRect"
                        Height="15"
                        Fill="Red"
                        Focusable="True" />
                    <ListBox x:Name="lit">
                        <ListBox.ItemContainerStyle>
                            <Style TargetType="ListBoxItem">
                                <Style.Triggers>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter Property="FontWeight" Value="Bold" />
                                        <Setter Property="Background" Value="Transparent" />
                                        <Setter Property="Foreground" Value="Red" />
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </ListBox.ItemContainerStyle>
                        <ListBoxItem HorizontalAlignment="Center">item1</ListBoxItem>
                        <ListBoxItem HorizontalAlignment="Center">item2</ListBoxItem>
                        <ListBoxItem HorizontalAlignment="Center">item3</ListBoxItem>
                    </ListBox>
                </StackPanel>
                <p:Attached.Operations>
                    <p:EventHandler Path="Loaded">
                        <p:ScriptHandler Path="@FindAncestor([Window], @AssociatedObject.PlacementTarget).LocationChanged">
                            @AssociatedObject.HorizontalOffset += 1;
                                    @AssociatedObject.HorizontalOffset -= 1;
                        </p:ScriptHandler>
                    </p:EventHandler>
                </p:Attached.Operations>
            </Popup>
        </StackPanel>

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    2017年7月7日 5:36
    版主