Silverlight ChildWindow没有正确重新定位

我正在使用ChildWindow(Silverlight),它还包含一些扩展器控件。在一种情况下,当扩展器控件扩展时,子窗口的底部向下扩展到底部的屏幕,但仍然在顶部留出空间。 如何重新定位子窗口以使其在屏幕中居中,就像我刚打开子窗口一样? (这很容易,但我认为不可行) (人工干预) 我已经浏览了ContentRoot的RenderTransform,我在该集合中有六个转换,其中两个是TranslateTransforms。如果我更新第一个的X / Y属性(不知道我应该改变哪两个)并使用整个TransformGroup更新RenderTransform属性,我成功地在屏幕上移动ChildWindow - 但它不是表现得像我期待的那样。 当扩展器控件扩展时,我也不知道为什么ChildWindow_SizeChanged事件不会触发。窗户的尺寸确实发生变化,为什么不开火呢? 好的 - 问题太多了,只需要回答第一个问题,剩下的就是填写我对WPF / Silverlight如何工作的了解...... 问候, 理查德     
已邀请:
通过此博客回答:http://www.kunal-chowdhury.com/2010/11/how-to-reposition-silverlight-child.html
/// <summary>
/// Centers the Silverlight ChildWindow in screen.
/// </summary>
/// <remarks>
/// 1) Visual TreeHelper will grab the first element within a ChildWindow - this is the Chrome (Title Bar, Close button, etc.)
/// 2) ContentRoot - is the first element within that Chrome for a ChildWindow - which is named this within the template of the control (Childwindow)
/// 3) Using the container (named ContentRoot), pull out all the "Transforms" which move, or alter the layout of a control
///   TranslateTransform - provides X,Y coordinates on where the control should be positioned
/// 4) Using a Linq expression, grab teh last TransLateTransfrom from the TransformGroup
/// 5) Reset the TranslateTransform to point 0,0 which should reference the ChildWindow to be the upper left of the window.  However, this is setting
///    is probably overridden by a default behaviour to always reset the window window to the middle of the screen based on it's size, and the size of the browser
///    I would have liked to animate this, but this likely requires a storyboard event that I don't have time for at this moment.
///    
/// This entire process to move, or alter a window in WPF was a total learning experience.
/// </remarks>
/// <param name="childWindow">The child window.
public static void CenterInScreen(this ChildWindow childWindow)
{
  var root = VisualTreeHelper.GetChild(childWindow, 0) as FrameworkElement;
  if (root == null) { return; }

  var contentRoot = root.FindName("ContentRoot") as FrameworkElement;
  if (contentRoot == null) { return; }

  var transformgroup = contentRoot.RenderTransform as TransformGroup;
  if (transformgroup == null) { return; }

  TranslateTransform transform = transformgroup.Children.OfType<TranslateTransform>().LastOrDefault();
  if (transform == null) { return; }

  transform.X = 0;
  transform.Y = 0;

}
}     

要回复问题请先登录注册