如何使WPF Combobox的Dropdown保持开放状态放置 [英] How to make WPF Combobox's Dropdown stays open & Placement

查看:1634
本文介绍了如何使WPF Combobox的Dropdown保持开放状态放置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让Combobox可编辑,并且下拉列表保持打开状态。

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

目前这些属性已设置:

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

每当用户点击输入文本框或者将焦点改为其他控件,dorpdown关闭。所以我更新了模板( WPF主题:BureauBlue)中包含的模板在这种特殊情况下,使得下拉列表保持打开状态的 Popup IsOpen =true移动窗口的位置,下拉菜单将自动更新其位置,保持在旧位置。

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

这是一个如何应用行为的例子: / p>

Here is an example how to apply the behavior:

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

这篇关于如何使WPF Combobox的Dropdown保持开放状态放置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆