如何使WPF组合框的下拉保持开放和放大器;放置组合、放大器、WPF

2023-09-03 20:39:33 作者:吉他及她

我想有在ComboBox可编辑和下拉保持打开状态。

I want to have the Combobox editable and with the dropdown stays open.

目前,这些属性被设置:

At the moment with these properties been set:

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

只要在输入文本框或聚焦用户点击更改为其他控件,dorpdown关闭。所以,我更新的模板(包含在 WPF主题:BureauBlue )有弹出 ISOPEN =真正的在这种特殊情况下,使下拉菜单保持打开状态,但现在当用户拖动和放大器;移动窗口的位置,下拉会的不可以自动更新其位置和留在老位置上。

Whenever the user click on the input textbox or the focus is changed to other controls, the dorpdown closes. So I updated the template (the one included in WPF Theme: BureauBlue) to have the Popup IsOpen="true" in this particular case that makes the dropdown stays open, but now when user drag&move the window's position, the dropdown will not update its position automatically and stay in the old position.

我怎样才能让处于打开状态时,它会自动更新它的位置

推荐答案

您可以使用这里描述的伎俩:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/27950e73-0007-4e0b-9f00-568d2db1d979

You can use the trick described here: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/27950e73-0007-4e0b-9f00-568d2db1d979

我创建了一个混合行为,可以很容易与任何弹出使用方法:

I created a Blend behavior that makes it easy to use with any popup:

/// <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));
    }
}

下面是一个例子如何应用的行为:

Here is an example how to apply the behavior:

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