2011-02-07 38 views
0

我有一个wpf窗口来托管控件。 窗口的高度和宽度设置为SizeToControl在wpf中呈现之前获取控制的实际宽度的问题

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

(所以我的窗口Top = ParentWindow.Top, and Left = ParentWindow.Left + ParentWindow.ActualWidth - control.ActualWidth使我的窗口定位在父窗口内,但它的右下角)

所以我需要设置窗口的顶部和左侧。要做到这一点,我需要的是被里面承载控制的实际宽度....但我只能得到这一次我实际上做,

Window.Show(control,parent)

有没有办法来解决这个问题?如何在实际显示之前获得控件的实际渲染宽度?

谢谢!

回答

1

你试过这种方法吗?

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; 
     } 
} 
相关问题