c# – 如何使WPF Combobox的Dropdown保持打开和放置
发布时间:2020-12-16 01:42:07 所属栏目:百科 来源:网络整理
导读:我希望Combobox可以编辑,并且下拉保持打开状态. 目前已设置这些属性: IsEditable="True" IsDropDownOpen="True" StaysOpenOnEdit="True" 每当用户单击输入文本框或焦点更改为其他控件时,dorpdown将关闭.所以我更新了模板(包含在WPF Theme:BureauBlue中的模
我希望Combobox可以编辑,并且下拉保持打开状态.
目前已设置这些属性: IsEditable="True" IsDropDownOpen="True" StaysOpenOnEdit="True" 每当用户单击输入文本框或焦点更改为其他控件时,dorpdown将关闭.所以我更新了模板(包含在WPF Theme:BureauBlue中的模板),在这种特殊情况下让Popup IsOpen =“true”使下拉列表保持打开状态,但是现在当用户拖动并移动窗口的位置时,下拉列表将不会更新它的位置自动保持原状. 如何在打开时自动更新其位置? 解决方法
您可以使用此处描述的技巧:
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> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |