2014-05-08 67 views
5

如果將StaysOpen更改爲「True」,彈出窗口會顯示,但當您單擊外部時它不會關閉,所以這不是我想要的。如果StaysOpen =「False」,彈出窗口不會打開

下面是相關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 = 「假」 現身?

+0

[StayOpen =「False」with inherited popups](http://stackoverflow.com/questions/21464557/stayopen-false-with-inherited-popups)可能有重複。請看這個鏈接問題的答案,以解決您的問題。 – Sheridan

+1

我不覺得你鏈接的問題的答案適用於這個問題。 – Kelsie

+1

您發佈的xaml是否完整?目前,您看起來像將行爲作爲孩子添加到「邊框」而不是「 ...」。 Viv

回答

0

事實證明,有一個祖先是不分青紅皁白地抓住鼠標捕獲。一切正常,現在我已經糾正了這個問題。另外,我可以通過設置StaysOpen =「True」,繼承Popup,以及連接到派生的Popup中的全局鼠標事件來解決此問題。當彈出窗口打開時,我會將處理程序附加到全局輸入事件。然後,當收到事件時,我會過濾它,以便我只響應鼠標左鍵按下事件。在處理這個事件時,如果鼠標不懸停彈出窗口,我會關閉彈出窗口並分離事件。它有效,但它顯然是一個骯髒的黑客,我很高興我沒有它的工作。

相關問題