Silverlight ChildWindow неправильно перемещается
Я использую ChildWindow (Silverlight), который также содержит некоторые элементы управления расширения. В одном случае, когда расширяется элемент управления расширением, нижняя часть дочернего окна расширяется вниз экрана внизу, но все равно оставляет место сверху.
Как я могу изменить положение дочернего окна, чтобы центрировать его на экране, как будто я только что открыл дочернее окно? (Это было бы легко, но я не думаю, что выполнимо)
(ручное вмешательство) Я прошел RenderTransform для ContentRoot, и у меня есть шесть преобразований в этой коллекции, 2 из которых - TranslateTransform. Если я обновлю свойства X/Y первого (не знаю, какое из двух я должен изменить) И обновлю свойство RenderTransform со всей TransformGroup, у меня будет успех в перемещении ChildWindow по экрану - но это не так ведя себя так, как я ожидаю.
Я также не знаю, почему событие ChildWindow_SizeChanged не срабатывает, когда расширяется элемент управления Expander. Окно изменяется по размеру, так почему оно не открывается?
Хорошо - слишком много вопросов, просто нужно ответить на первый вопрос, а остальные - чтобы мои знания о том, как работает WPF/Silverlight...
С уважением, Ричард
1 ответ
Ответ через этот блог: 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;
}
}