如果我将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”?
答案 0 :(得分:0)
事实证明,有一个祖先不分青红皂白地抓住了鼠标下来的捕获物。现在我已经纠正了这个问题,一切正常。
顺便说一句,通过设置StaysOpen =&#34; True&#34;,继承Popup,并挂钩到派生Popup中的全局鼠标事件,我能够解决这个问题。当弹出窗口打开时,我将处理程序附加到全局输入事件。然后,当收到一个事件时,我会对其进行过滤,以便我只响应鼠标左键按下事件。在处理此事件时,如果鼠标没有悬停弹出窗口,我会关闭弹出窗口并分离事件。它有效,但显然是一个肮脏的黑客,我很高兴我没有它就能使用它。