2011-03-17 177 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

相关问题