【问题标题】:Popup doesn't open if StaysOpen="False"如果 StaysOpen="False" 则不会打开弹出窗口
【发布时间】:2014-06-26 02:36:57
【问题描述】:

如果我将 StaysOpen 更改为“True”,则会显示弹出窗口,但是当您在其外部单击时它不会关闭,所以这不是我想要的。

这里是相关的 XAML 代码:

<Border x:Name="popupPlacementTarget">
    <i:Interaction.Behaviors>
        <local:PopupBehavior>
            <local:PopupBehavior.Popup>
                <Popup PlacementTarget="{Binding ElementName=popupPlacementTarget}"
                       Placement="MousePoint"
                       StaysOpen="False">
                    <ContentPresenter Content="{Binding SomeContent}" ContentTemplate="{StaticResource SomeContentTemplate}" />
                </Popup>
            <local:PopupBehavior.Popup>
        </local:PopupBehavior>
    </i:Interaction.Behaviors>
</Border>

这里是 PopupBehavior 代码:

public class PopupBehavior : Behavior<UIElement>
{
    #region Dependency Properties

    public static readonly DependencyProperty PopupProperty = DependencyProperty.Register(
        "Popup", typeof(Popup), typeof(PopupBehavior), new FrameworkPropertyMetadata(default(Popup)));

    #endregion Dependency Properties

    #region Properties

    public Popup Popup
    {
        get { return (Popup)this.GetValue(PopupProperty); }
        set { this.SetValue(PopupProperty, value); }
    }

    #endregion Properties

    #region Protected Methods

    protected override void OnAttached()
    {
        base.OnAttached();

        this.AssociatedObject.MouseDown += this.OnMouseDown;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();

        this.AssociatedObject.MouseDown -= this.OnMouseDown;
    }

    #endregion Protected Methods

    #region Private Methods

    private void OnMouseDown(object sender, MouseButtonEventArgs eventArgs)
    {
        var popup = this.Popup;
        if (popup == null)
        {
            return;
        }

        this.Popup.IsOpen = true;
    }

    #endregion Private Methods
}

知道为什么我的弹出窗口不会显示为 StaysOpen="False" 吗?

【问题讨论】:

  • StayOpen="False" with inherited popups 的可能副本。请查看我对此链接问题的回答,了解您的问题的解决方案。
  • 我觉得你链接的问题的答案不适用于这个问题。
  • 您发布的 xaml 是否完整?目前,您似乎将Behavior 作为孩子添加到Border,而不是&lt;i:Interaction.Behaviors&gt;...&lt;/i:Interaction.Behaviors&gt;
  • 这是一个错字,它在交互行为中。我会编辑。
  • @Kelsie,您很少遇到与另一个问题完全相同的问题。关键是,如果您只想尝试一下,链接的帖子中有一个可行的解决方案。

标签: c# wpf xaml


【解决方案1】:

原来有一个祖先在鼠标按下时不分青红皂白地抓捕。现在我已经纠正了这个问题,一切正常。

顺便说一句,我可以通过设置 StaysOpen="True"、继承 Popup 以及挂钩到派生的 Popup 中的全局鼠标事件来解决这个问题。当弹出窗口打开时,我会将处理程序附加到全局输入事件。然后,当收到一个事件时,我会对其进行过滤,以便我只响应鼠标左键按下事件。在处理此事件时,如果鼠标未悬停在弹出窗口上,我将关闭弹出窗口并分离事件。它有效,但它显然是一个肮脏的黑客,我很高兴我没有它就可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多