1

我正在使用还包含一些扩展器控件的 ChildWindow (Silverlight)。在一种情况下,当展开器控件展开时,子窗口的底部会向下展开到底部的屏幕之外,但仍会在顶部留出空间。

如何重新定位子窗口以使其在屏幕中居中,就好像我刚刚打开了子窗口一样?(这很容易,但我认为不可行)

(手动干预)我已经完成了 ContentRoot 的 RenderTransform,我在该集合中有六个变换,其中两个是 TranslateTransforms。如果我更新第一个的 X/Y 属性(不知道我应该更改两个中的哪一个)并使用整个 TransformGroup 更新 RenderTransform 属性,我可以成功地在屏幕上移动 ChildWindow - 但它不是表现得像我预期的那样。

我也不知道为什么在 Expander 控件展开时 ChildWindow_SizeChanged 事件不会触发。窗口的大小确实发生了变化,那么为什么它不触发呢?

好的 - 问题太多了,只需要回答第一个问题,剩下的就是填写我对 WPF/Silverlight 工作原理的了解...

问候,理查德

4

1 回答 1

3

通过此博客回答: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;

}

}

于 2011-06-20T21:05:39.440 回答