The presence of RenderTransform in the ChildWindow template seems to be to blame. TransformGroup is part of the default template, allowing you to move around the window.
Here's a hack to reset the conversion after changing the layout:
//after you do some change to the childwindow layout sp.Children.Add(new Button() { Content = "a" }); Dispatcher.BeginInvoke(() => { //reset the transform to zero (this.GetTemplateChild("ContentRoot") as Grid).RenderTransform = new TransformGroup() { Children = { new ScaleTransform(), new SkewTransform(), new RotateTransform(), new TranslateTransform() } }; });
or more automatically:
public override void OnApplyTemplate() { base.OnApplyTemplate(); var contentRoot = this.GetTemplateChild("ContentRoot") as FrameworkElement; contentRoot.LayoutUpdated += contentRoot_LayoutUpdated; } void contentRoot_LayoutUpdated(object sender, EventArgs e) { var contentRoot = this.GetTemplateChild("ContentRoot") as FrameworkElement; var tg = contentRoot.RenderTransform as TransformGroup; var tts = tg.Children.OfType<TranslateTransform>(); foreach (var t in tts) { tX = 0; tY = 0; } }
LayoutUpdated is often called, so you can check if contentRoot.ActualWidth and ActualHeight have changed, do you really need to destroy the transform.
foson source share