问题
I am using the ChildWindow (Silverlight) which also contains some expander controls. In one case, when the expander control expands, the bottom of the child window expands down out the screen on the bottom, but still leaves room on the top.
How can I re-position a child window in order to center it in the screen, as if I just opened the childwindow? (That would be easy, but I don't think doable)
(manual intervention) I've gone through RenderTransform of the the ContentRoot, and I have six transforms in that collection, 2 of which are TranslateTransforms. If I update the X/Y properties of the first one (don't know which of the two I should be changing) AND update the RenderTransform property with the entire TransformGroup, I have success in moving the ChildWindow around the screen - but it's not behaving as I am expecting.
I also dont' know why the ChildWindow_SizeChanged event does not fire, when the Expander control expands. The window does change in size, so why does it not fire?
Ok - too many questions, just need the first one answered, the rest are to fill in my knowledge of how WPF/Silverlight is working...
Regards, Richard
回答1:
Answer via this blog: 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;
}
}
来源:https://stackoverflow.com/questions/5466232/silverlight-childwindow-does-not-reposition-correctly