开发者

Problem getting the actual width of control before rendering in wpf

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

Now I need to posit开发者_如何学JAVAion 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!


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;
        }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜