Silverlight: move child window to center

The content inside the child window changes, which leads to the fact that my child window loses its alignment in the center .... After the content has changed, the child window will still be moved to the center ... I tried the following: it does not work:

this.horizontalalignment = horizontalalignment.center; this.verticalalignment = verticalalignment.center; 

thanks

+4
source share
2 answers

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.

+8
source

code

 public partial class DialogOptions : ChildWindow { public DialogOptions() { InitializeComponent(); Loaded += (sender, args) => { VerticalAlignment = VerticalAlignment.Top; this.SetWindowPosition(new Point(0, 200)); }; } } 

And extension:

  public static void SetWindowPosition(this ChildWindow childWindow, Point point) { var root = VisualTreeHelper.GetChild(childWindow, 0) as FrameworkElement; if (root == null) { return; } var contentRoot = root.FindName("ContentRoot") as FrameworkElement; if (contentRoot == null) { return; } var group = contentRoot.RenderTransform as TransformGroup; if (group == null) { return; } TranslateTransform translateTransform = null; foreach (var transform in group.Children.OfType<TranslateTransform>()) { translateTransform = transform; } if (translateTransform == null) { return; } translateTransform.X = point.X; translateTransform.Y = point.Y; } 
0
source

All Articles