2011-03-17 209 views
3

我想讓Combobox可編輯並且下拉菜單保持打開狀態。如何使WPF Combobox的下拉菜單保持打開狀態

在具有這些特性的時刻被設置:

IsEditable="True" IsDropDownOpen="True" StaysOpenOnEdit="True" 

只要輸入文本框或聚焦用戶點擊更改爲其他控件,dorpdown關閉。所以我更新的模板(以WPF Theme包括一個:BureauBlue),以在使下拉保持打開某些特定情況下PopupIsOpen="true",但現在,當用戶拖動&移動窗口的位置,下拉菜單會更新其位置自動和保持在舊的位置。

如何在打開時自動更新位置

回答

7

您可以使用下面介紹的技巧:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/27950e73-0007-4e0b-9f00-568d2db1d979

我創建了一個Blend behavior,可以很容易與任何彈出使用:

/// <summary> 
/// A behavior that forces the associated popup to update its position when the <see cref="Popup.PlacementTarget"/> 
/// location has changed. 
/// </summary> 
public class AutoRepositionPopupBehavior : Behavior<Popup> { 
    public Point StartPoint = new Point(0, 0); 
    public Point EndPoint = new Point(0, 0); 

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

     if (AssociatedObject.PlacementTarget != null) { 
      AssociatedObject.PlacementTarget.LayoutUpdated += OnPopupTargetLayoutUpdated; 
     } 
    } 

    void OnPopupTargetLayoutUpdated(object sender, EventArgs e) { 
     if (AssociatedObject.IsOpen) { 
      ResetPopUp(); 
     } 
    } 

    public void ResetPopUp() { 
     // The following trick that forces the popup to change it's position was taken from here: 
     // http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/27950e73-0007-4e0b-9f00-568d2db1d979 
     Random random = new Random(); 
     AssociatedObject.PlacementRectangle = new Rect(new Point(random.NextDouble()/1000, 0), new Size(75, 25)); 
    } 
} 

下面是一個例子如何應用行爲:

<Popup ...> 
    <i:Interaction.Behaviors> 
     <Behaviors:AutoRepositionPopupBehavior /> 
    </i:Interaction.Behaviors> 
</Popup> 
+0

感謝您的回答,我已經實現了這個行爲,但有時候'OnPopupTargetLayoutUpdated'沒有觸發(例如當我移動窗口時),任何建議蒸發散? – Bolu 2011-03-17 14:11:43

+0

只是爲了那裏的新手,你需要設置你的彈出窗口的放置目標。感謝PG,這個工作非常好。 – 2014-07-17 16:01:50

相關問題