弹出窗口出现在其父级的右下角
我正在尝试设计一个弹出窗口,它将出现在其 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);
}
}
是否有任何其他事件我可以用来捕获我想要的东西? 我基本上想将 HorizontalOffset
设置为 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常我将
PlacementTarget
设置为Bottom
或Right
,然后应用RenderTransform
来移动Popup
按剩余价值。例如,我可能使用
Placement=Bottom
,然后使用RenderTransform
将弹出窗口(Window.Width - Popup.Width)
移动到向右,Popup.Height
向上。您甚至可能不需要根据 Popup 高度/宽度重新调整,因为 MSDN 规定 Popups 不允许在屏幕外显示,并且它会自动调整它们的位置以保持它们可见。请确保您使用 RenderTransform 而不是
LayoutTransform
,因为RenderTransforms
在 Popup 渲染后应用,所以ActualHeight
和ActualWidth
将大于 0。Usually I set the
PlacementTarget
to eitherBottom
orRight
, then apply aRenderTransform
which shifts thePopup
by the remaining value.For example, I might use
Placement=Bottom
, then use aRenderTransform
to shift the popup(Window.Width - Popup.Width)
to the right, andPopup.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 visibleBe sure you use a
RenderTransform
instead of aLayoutTransform
, becauseRenderTransforms
get applied after the Popup gets Rendered, so theActualHeight
andActualWidth
will be greater than 0.