C# 如何制作WPF组合框';s下拉列表保持打开状态&;安置
我想让组合框可编辑,下拉列表保持打开状态 在设置这些属性时:C# 如何制作WPF组合框';s下拉列表保持打开状态&;安置,c#,.net,wpf,combobox,drop-down-menu,C#,.net,Wpf,Combobox,Drop Down Menu,我想让组合框可编辑,下拉列表保持打开状态 在设置这些属性时: IsEditable="True" IsDropDownOpen="True" StaysOpenOnEdit="True" 每当用户单击输入文本框或焦点更改为其他控件时,dorpdown将关闭。因此,我更新了模板(包含在:BureauBlue中的模板),使其具有弹出窗口IsOpen=“true”,在这种特殊情况下,它使下拉列表保持打开状态,但现在当用户拖动并移动窗口位置时,下拉菜单将不自动更新其位置,并保持在原来的位置 如何使其
IsEditable="True" IsDropDownOpen="True" StaysOpenOnEdit="True"
每当用户单击输入文本框或焦点更改为其他控件时,dorpdown将关闭。因此,我更新了模板(包含在:BureauBlue中的模板),使其具有弹出窗口
IsOpen=“true”
,在这种特殊情况下,它使下拉列表保持打开状态,但现在当用户拖动并移动窗口位置时,下拉菜单将不自动更新其位置,并保持在原来的位置
如何使其在打开时自动更新其位置?您可以使用此处介绍的技巧: 我创建了一个可以轻松使用任何弹出窗口的:
/// <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));
}
}
//
///当
///位置已更改。
///
公共类自动存储PopuBehavior:行为{
公共点起始点=新点(0,0);
公共点端点=新点(0,0);
受保护的覆盖无效附加(){
base.onatached();
if(AssociatedObject.PlacementTarget!=null){
AssociatedObject.PlacementTarget.LayoutUpdated+=OnPoputTargetLayoutUpdated;
}
}
void onPoputTargetLayoutUpdate(对象发送方,事件参数e){
if(AssociatedObject.IsOpen){
重置弹出窗口();
}
}
公共无效重置弹出窗口(){
//以下迫使弹出窗口改变其位置的技巧就是从这里开始的:
// http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/27950e73-0007-4e0b-9f00-568d2db1d979
随机=新随机();
AssociatedObject.PlacementRectangle=新矩形(新点(random.NextDouble()/1000,0),新大小(75,25));
}
}
下面是一个如何应用行为的示例:
<Popup ...>
<i:Interaction.Behaviors>
<Behaviors:AutoRepositionPopupBehavior />
</i:Interaction.Behaviors>
</Popup>
谢谢你的回答,我已经实现了这个行为,但是有时候OnPoputTargetLayoutUpdate
没有启动(例如,当我移动窗口时),有什么建议吗?对于新手来说,你需要设置弹出窗口的位置目标,这样才能工作。谢谢,PG,这真是太棒了。