在 wpf 中渲染之前获取控件的实际宽度时出现问题

发布于 2024-10-16 09:37:19 字数 431 浏览 4 评论 0原文

我有一个承载控件的 wpf 窗口。 窗口的高度和宽度设置为 SizeToControl

现在我需要相对于其父窗口的位置定位该窗口。(基本上是右上角位置)。

(所以我的窗口 Top = ParentWindow.Top, and Left = ParentWindow.Left + ParentWindow.ActualWidth - control.ActualWidth 以便我的窗口位于父窗口内部但位于其右角)

所以我会需要设置窗口的顶部和左侧。为此,我需要其中托管的控件的实际宽度......但我只能在实际执行时获得此宽度,

Window.Show(control,parent)

是否有如何解决这个问题?如何在实际显示控件之前获取控件的实际渲染宽度?

谢谢!

I have a wpf window that hosts a control.
The window's height and width are set to SizeToControl.

Now I need to position this Window relative to the position of its parent window.(basically the Top right position).

(So my windows Top = ParentWindow.Top, and Left = ParentWindow.Left + ParentWindow.ActualWidth - control.ActualWidth so that my window is positioned inside the parent window but to its right corner)

So i will need to set the Top and Left of the window. To do this I need the Actual Width of the control that is being hosted inside it....but I can only get this once I actually do,

Window.Show(control,parent)

Is there a way to get around this problem? How do I get the actual rendered width of the control before it is actually shown?

Thanks!

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

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

发布评论

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

评论(1

眸中客 2024-10-23 09:37:19

你尝试过这种方法吗?

    public partial class ShellWindow : Window
    {
        public ShellWindow(IShellPresentationModel model)
        {
            InitializeComponent();
            this.Loaded += ShellWindow_Loaded;
        }

        void ShellWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var innerWindow = new InnerWindow();
            innerWindow.Owner = this;
            innerWindow.Loaded += InnerWindow_Loaded;
            innerWindow.Show();
        }

        void InnerWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var w = (InnerWindow)sender;
            w.Owner = this;
            w.Top = this.Top;
            w.Left = this.Left + this.ActualWidth - w.ActualWidth;
        }
}

Have you tried this approach?

    public partial class ShellWindow : Window
    {
        public ShellWindow(IShellPresentationModel model)
        {
            InitializeComponent();
            this.Loaded += ShellWindow_Loaded;
        }

        void ShellWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var innerWindow = new InnerWindow();
            innerWindow.Owner = this;
            innerWindow.Loaded += InnerWindow_Loaded;
            innerWindow.Show();
        }

        void InnerWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var w = (InnerWindow)sender;
            w.Owner = this;
            w.Top = this.Top;
            w.Left = this.Left + this.ActualWidth - w.ActualWidth;
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文