弹出窗口出现在其父级的右下角

发布于 2024-12-22 22:08:00 字数 1467 浏览 5 评论 0原文

我正在尝试设计一个弹出窗口,它将出现在其 PlacementTarget 的右下角。

让我们承认您将其 PlacementTarget 设置为 Window >,好吧,弹出窗口将充当经典的烤面包机通知。

鉴于 WPF 不够智能,无法为我们提供“角落”解决方案,我正在尝试实现一个继承自 Popup 的新控件,它将自身放置在适当的位置。

这是我的第一个想法:处理 Loaded 事件来确定应该将弹出窗口放置在哪里。 问题?我不想为弹出窗口提供任何固定尺寸,它应该根据显示的文本自行调整大小。

但是,当引发 Loaded 事件时,我无法获取 ActualWidth 属性。 当引发 Opened 事件时我也无法获得它。

这是到目前为止的代码草案:

public class ExceptionPopup : Popup
{
       public ExceptionPopup()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(ExceptionPopup_Loaded);
        }

        void ExceptionPopup_Loaded(object sender, RoutedEventArgs e)
        {
            if (PlacementTarget != null)
            {
                if (PlacementTarget is FrameworkElement)
                {
                    parentWidth = (PlacementTarget as FrameworkElement).ActualWidth;
                    parentHeight = (PlacementTarget as FrameworkElement).ActualHeight;
                }
            }
        }

        protected override void OnOpened(EventArgs e)
        {
            this.HorizontalOffset = parentWidth;
            this.VerticalOffset = parentHeight;
            base.OnOpened(e);
        }
}

是否有任何其他事件我可以用来捕获我想要的东西? 我基本上想将 Horizo​​ntalOffset 设置为 parentWidth - ActualWidth/2 ,高度相同:) 有什么想法吗?

谢谢!

I'm trying to design a Popup which will appear on the bottom-right corner of its PlacementTarget

Let's admit that you set its PlacementTarget to a Window, well, the Popup will act as classic toaster notifications.

Given the fact that WPF is not smart enough to provide us a "corner" solution, I'm trying to implement a new control, inheriting from Popup , which will place itself at the appropriate location.

Here is my first idea: work on Loaded event to determine where should I place the Popup.
Problem? I don't want to give any fixed dimensions to the popup, which is supposed to size itself according to the text displayed.

However, I can't get the ActualWidth property when Loaded event is raised.
I can't have it either when Opened event is raised.

Here is the draft code so far:

public class ExceptionPopup : Popup
{
       public ExceptionPopup()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(ExceptionPopup_Loaded);
        }

        void ExceptionPopup_Loaded(object sender, RoutedEventArgs e)
        {
            if (PlacementTarget != null)
            {
                if (PlacementTarget is FrameworkElement)
                {
                    parentWidth = (PlacementTarget as FrameworkElement).ActualWidth;
                    parentHeight = (PlacementTarget as FrameworkElement).ActualHeight;
                }
            }
        }

        protected override void OnOpened(EventArgs e)
        {
            this.HorizontalOffset = parentWidth;
            this.VerticalOffset = parentHeight;
            base.OnOpened(e);
        }
}

Is there any other event I could use to catch what I want here?
I'd basically like to set HorizontalOffset to parentWidth - ActualWidth/2 , same for height :)
Any idea?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

貪欢 2024-12-29 22:08:00

通常我将 PlacementTarget 设置为 BottomRight,然后应用 RenderTransform 来移动 Popup 按剩余价值。

例如,我可能使用 Placement=Bottom,然后使用 RenderTransform 将弹出窗口 (Window.Width - Popup.Width) 移动到向右,Popup.Height 向上。您甚至可能不需要根据 Popup 高度/宽度重新调整,因为 MSDN 规定 Popups 不允许在屏幕外显示,并且它会自动调整它们的位置以保持它们可见。

请确保您使用 RenderTransform 而不是 LayoutTransform,因为 RenderTransforms 在 Popup 渲染后应用,所以 ActualHeightActualWidth 将大于 0。

Usually I set the PlacementTarget to either Bottom or Right, then apply a RenderTransform which shifts the Popup by the remaining value.

For example, I might use Placement=Bottom, then use a RenderTransform to shift the popup (Window.Width - Popup.Width) to the right, and Popup.Height upwards. You might not even need to re-adjust based on the Popup Height/Width becauase MSDN says that Popups are not allowed to be displayed off screen, and it will automatically adjust their placement to keep them visible

Be sure you use a RenderTransform instead of a LayoutTransform, because RenderTransforms get applied after the Popup gets Rendered, so the ActualHeight and ActualWidth will be greater than 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文