我如何可以移动一个WPF弹出时,其锚元素的动作? [英] How can I move a WPF Popup when its anchor element moves?

查看:134
本文介绍了我如何可以移动一个WPF弹出时,其锚元素的动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样定义的弹出窗口:

I have a Popup defined like this:

<Popup
    Name="myPopup"
    StaysOpen="True"
    Placement="Bottom"
    PlacementRectangle="0,20,0,20"
    PlacementTarget="{Binding ElementName=myPopupAnchor}">
    <TextBlock ... />
</Popup>

我添加事件处理程序为事件的的MouseEnter myPopupAnchor 元素和鼠标离开。这两个事件处理程序切换弹出窗口的可见性。

I have added event handlers to the myPopupAnchor element for the events MouseEnter and MouseLeave. The two event handlers toggle the popup's visibility.

我的问题是myPopupAnchor的位置时,弹出窗口首先显示或隐藏,然后再次显示为只读。如果锚移动,弹出没有。

My problem is the position of myPopupAnchor is only read when the popup is first shown, or hidden and then shown again. If the anchor moves, the popup does not.

我在寻找各种方法来解决这个问题,我想移动弹出。我可以通知WPF的 PlacementTarget 绑定已发生变化,需重新读取?我可以手动设置弹出窗口的位置?

I'm looking for ways to work around this, I want a moving Popup. Can I notify WPF that the PlacementTarget binding has changed and should be read again? Can I manually set the popup's position?

目前,我有一个非常粗糙的变通方法,包括关闭,然后再次打开弹出窗口,这会导致一些问题,重绘

Currently, I have a very crude workaround that involves closing and then opening the popup again, which causes some repainting issues.

推荐答案

我看着一对夫妇的选择和样本在那里。这似乎工作最适合我的事情是为凸点导致该弹出祭出自身的属性之一。我使用的属性是Horizo​​ntalOffset。

I looked at a couple options and samples out there. The thing that seems to work best for me is to "bump" one of the properties that causes the Popup to reposition itself on its own. The property that I used is HorizontalOffset.

我将其设置为(自己+ 1),然后将其设置回原始值。我这样做在当重新定位的窗口中运行的事件处理程序。

I set it to (itself + 1) and then set it back the original value. I do this in an event handler that runs when the window is repositioned.

// Reference to the PlacementTarget.
DependencyObject myPopupPlacementTarget;

// Reference to the popup.
Popup myPopup; 

Window w = Window.GetWindow(myPopupPlacementTarget);
if (null != w)
{
    w.LocationChanged += delegate(object sender, EventArgs args)
    {
        var offset = myPopup.HorizontalOffset;
        myPopup.HorizontalOffset = offset + 1;
        myPopup.HorizontalOffset = offset;
    };
}

在移动窗口,弹出将重新定位。在Horizo​​ntalOffset了微妙的变化是不是注意到,因为窗口和弹出已反正感动。

When the window is moved, the popup will reposition. The subtle change in the HorizontalOffset is not noticed because the window and popup are already moving anyway.

我仍然在评估一个弹出控件是否在情况下,控制保持其他交互期间开放的最佳选择。我在想,<一个href=\"http://stackoverflow.com/questions/978186/binding-a-popup-to-another-controls-relative-screen-position\">Ray伯恩斯建议把这个东西在装饰器层似乎像某些情况下一个不错的办法。

I'm still evaluating whether a popup control is the best option in cases where the control stays open during other interaction. I'm thinking that Ray Burns suggestion to put this stuff in an Adorner layer seems like a good approach for some scenarios.

这篇关于我如何可以移动一个WPF弹出时,其锚元素的动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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