简体   繁体   English

如何使用触发按钮关闭弹出窗口

[英]How to close Popup with trigger button

Hi I need the trigger button to open and also close the popup, and the popup should close whenever a mouse click occurs outside the popup. 嗨,我需要触发按钮打开并关闭弹出窗口,只要在弹出窗口外单击鼠标,弹出窗口就应该关闭。

Here's my XAML: 这是我的XAML:

<Button x:Name="About"
                    Height="50"
                    Margin="0,-30,5,0"
                    HorizontalAlignment="Right"
                    Style="{StaticResource AboutButtonStyle}" />
<Popup HorizontalOffset="-300"
                   IsOpen="{Binding IsAboutPopupOpen, Mode=TwoWay}"
                   Placement="RelativePoint"
                   PlacementTarget="{Binding ElementName=About}"
                   StaysOpen="False"
                   VerticalOffset="-125">
    <Border Padding="10"
                        Background="White"
                        BorderBrush="{StaticResource SeparatorColorBrush}"
                        BorderThickness="1">
        <TextBlock>Some Text</TextBlock>
    </Border>
</Popup>

In my AboutViewModel I have implemented a property, IsAboutPopupOpen : 在我的AboutViewModel中,我实现了一个属性IsAboutPopupOpen

private bool isAboutPopupOpen;

public bool IsAboutPopupOpen
{
    get
    {
        return this.isAboutPopupOpen;
    }

    set
    {
        if (value != this.isAboutPopupOpen)
        {
            this.isAboutPopupOpen = value;
            this.NotifyOfPropertyChange(() => IsAboutPopupOpen);
        }
    }
}

public void About()
{
    IsAboutPopupOpen = true;
}

The issue is, when the popup is open, and I click the About button, the PopUp closes and opens again. 问题是,当弹出窗口打开时,当我单击“关于”按钮时,弹出窗口关闭并再次打开。 It should close. 它应该关闭。 Other than that, the behavior is right. 除此之外,行为是正确的。

I have searched for a simple solution to this, and cannot seem to find it. 我正在寻找一个简单的解决方案,似乎找不到它。 This should be a common question. 这应该是一个常见的问题。 Oh and I'm using Caliburn.Micro, but that shouldn't matter, I don't think. 哦,我正在使用Caliburn.Micro,但这没关系,我认为。 Thanks 谢谢

@Xiaoy312 pointed me in the right direction. @ Xiaoy312为我指明了正确的方向。 First I decided to use a ToggleButton: 首先,我决定使用ToggleButton:

    <ToggleButton
        x:Name="ShowAboutPopup"
        Grid.Row="1"
        Height="50"
        Margin="0,-30,5,0"
        HorizontalAlignment="Right"
        Grid.ZIndex="1000"
        Style="{StaticResource AboutButtonStyle}" />

Then I decided to connect my ToggleButton to the popup via the IsOpen attribute, and setting StaysOpen to false: 然后,我决定通过IsOpen属性将ToggleButton连接到弹出窗口,并将StaysOpen设置为false:

    <Popup
        Grid.Row="1"
        Closed="AboutPopup_OnClosed"
        HorizontalOffset="-300"
        IsOpen="{Binding ElementName=ShowAboutPopup, Path=IsChecked, Mode=OneWay}"
        Placement="RelativePoint"
        PlacementTarget="{Binding ElementName=ShowAboutPopup}"
        StaysOpen="False"
        VerticalOffset="-125">
...
    </Popup>

And finally I implemented AboutPopup_OnClosed in the code-behind: 最后,我在后面的代码中实现了AboutPopup_OnClosed:

    private void AboutPopup_OnClosed(object sender, EventArgs e)
    {
        if (!object.Equals(this.ShowAboutPopup, Mouse.DirectlyOver))
        {
            this.ShowAboutPopup.IsChecked = false;
        }
    }

Hope this helps others that may have struggled with this. 希望这可以帮助其他可能为此苦恼的人。 The bottom line is that you should typically use a toggle button to open and close a popup. 最重要的是,您通常应该使用切换按钮来打开和关闭弹出窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM