如何使WPF Combobox的下拉菜单保持打开状态&放置

2024-01-02

我想让组合框可编辑并且下拉菜单保持打开状态。

目前已设置这些属性:

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

每当用户单击输入文本框或焦点更改为其他控件时,dorpdown 就会关闭。所以我更新了模板(包含在WPF主题 http://wpf.codeplex.com/wikipage?title=WPF%20Themes: BureauBlue) 拥有Popup IsOpen="true"在这种特殊情况下,使下拉菜单保持打开状态,但现在当用户拖动并移动窗口的位置时,下拉菜单将not自动更新其位置并stay在旧位置。

如何让它在打开时自动更新其位置?


您可以使用此处描述的技巧:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/27950e73-0007-4e0b-9f00-568d2db1d979 http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/27950e73-0007-4e0b-9f00-568d2db1d979

我创建了一个混合行为 http://blogs.msdn.com/b/dphill/archive/2009/09/25/blend-behaviors.aspx这使得它可以轻松地与任何弹出窗口一起使用:

/// <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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使WPF Combobox的下拉菜单保持打开状态&放置 的相关文章

随机推荐