wpf 中的弹出窗口和切换按钮交互

发布于 2024-11-03 18:15:38 字数 423 浏览 0 评论 0原文

我有一个包含切换按钮和弹出窗口的控件。单击 ToggleButton 时,会出现弹出窗口。当 ToggleButton 未选中时,弹出窗口应关闭。此外,单击远离弹出窗口应导致其关闭,并导致切换按钮取消选中。

我通过将 Popup 的 StaysOpen 属性设置为 false,并将切换按钮的 IsChecked 属性设置为双向绑定到 Popup 的 IsOpen 属性来进行设置。

一切都很好,除了一种情况 - 选中按钮并打开弹出窗口后,单击按钮不会导致弹出窗口关闭,或者按钮返回到未选中状态。

我相信这一定是因为单击按钮会导致 Popup 的 StaysOpen 逻辑将 Popup 的 IsOpen 属性设置为 false。反过来,这会将切换按钮设置为未选中。这必须在处理我点击按钮之前发生 - 因此点击会重新检查按钮,即竞争条件。

知道如何获得我想要的行为吗?

I have a control that contains a Togglebutton and a Popup. When the ToggleButton is clicked on, the popup appears. When the ToggleButton is unchecked, the popup should close. Additionally, clicking away from the popup should cause it to close, and cause the Togglebutton to uncheck.

I've set this up by setting the StaysOpen property of the Popup to false, and setting the IsChecked property of the toggle button to be two-way bound to the IsOpen property of the Popup.

All is well, apart from one case - with the button checked and the popup open, clicking the button does not cause the popup to close, or the button to return to unchecked.

I believe this must be because clicking the button causes the StaysOpen logic of the Popup to set the Popup's IsOpen property to false. In turn, this sets the Togglebutton to unchecked. This must happen before my click on the button is processed - so the click re-checks the button, ie a race condition.

Any idea how I can get the behaviour I want?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

写下不归期 2024-11-10 18:15:38

如果您的假设是正确的,您需要一个如下所示的自定义 Popup 类:

public class MyPopup : Popup {
    protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e) {
        bool isOpen = this.IsOpen;
        base.OnPreviewMouseLeftButtonDown(e);

        if (isOpen && !this.IsOpen)
            e.Handled = true;
    }
}

您可能需要从 if 语句中删除 !this.IsOpen。如果您改用 MyPopup,它将阻止 MouseLeftButtonDown 事件到达 ToggleButton。

If your assumption is correct, you'd need a custom Popup class like the following:

public class MyPopup : Popup {
    protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e) {
        bool isOpen = this.IsOpen;
        base.OnPreviewMouseLeftButtonDown(e);

        if (isOpen && !this.IsOpen)
            e.Handled = true;
    }
}

You may need to remove the !this.IsOpen from the if-statement. If you use MyPopup instead, it will prevent the MouseLeftButtonDown event from reaching the ToggleButton.

锦爱 2024-11-10 18:15:38

上述两种解决方案都有问题。这是另一个使用事件处理程序而不是绑定的解决方案,但确实避免了 svick 在 MyPopup 解决方案中指出的丢失点击问题以及 ClickMode=Press 的问题。
xaml 看起来像:

<ToggleButton Name="OptionsButton" Checked="OptionsButton_OnChecked" Unchecked="OptionsButton_OnUnchecked" />
<Popup Name="OptionsPopup" StaysOpen="False" Closed="OptionsPopup_OnClosed"/>

和代码:

    void OptionsPopup_OnClosed(object sender, EventArgs e)
    {
        if (OptionsButton != Mouse.DirectlyOver)
            OptionsButton.IsChecked = false;
    }

    void OptionsButton_OnChecked(object sender, RoutedEventArgs e)
    {
        OptionsPopup.IsOpen = true;
    }

    void OptionsButton_OnUnchecked(object sender, RoutedEventArgs e)
    {
        OptionsPopup.IsOpen = false;
    }

Both the solutions above have issues. Here's another solution that uses event handlers instead of binding, but does avoid the lost click issue that svick pointed out with the MyPopup solution and the issues with ClickMode=Press.
The xaml looks like:

<ToggleButton Name="OptionsButton" Checked="OptionsButton_OnChecked" Unchecked="OptionsButton_OnUnchecked" />
<Popup Name="OptionsPopup" StaysOpen="False" Closed="OptionsPopup_OnClosed"/>

And code :

    void OptionsPopup_OnClosed(object sender, EventArgs e)
    {
        if (OptionsButton != Mouse.DirectlyOver)
            OptionsButton.IsChecked = false;
    }

    void OptionsButton_OnChecked(object sender, RoutedEventArgs e)
    {
        OptionsPopup.IsOpen = true;
    }

    void OptionsButton_OnUnchecked(object sender, RoutedEventArgs e)
    {
        OptionsPopup.IsOpen = false;
    }
此岸叶落 2024-11-10 18:15:38

解决方案是在 Popup 的 IsOpen 属性和 ToggleButton 的 IsChecked 属性之间设置 TwoWay 绑定 - 然后设置的 ClickMode 属性切换按钮按下。瞧!

The solution is to set a TwoWay binding between the IsOpen property of the Popup and the IsChecked property of the ToggleButton - THEN set the ClickMode property of the ToggleButton to Press. Voila !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文