子窗口内的内容发生变化,这导致我的子窗口失去了居中对齐...。内容发生变化后,无论如何都可以将子窗口重新定位到中心...我尝试了以下操作,它做到了不工作:
this.horizontalalignment = horizontalalignment.center;
this.verticalalignment = verticalalignment.center;
谢谢
最佳答案
ChildWindow 模板上的 RenderTransform 似乎是罪魁祸首。 TransformGroup 是默认模板的一部分,允许您在窗口中移动。
这是在更改布局后重置转换的 hack:
//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() }
};
});
或更自动地:
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)
{
t.X = 0; t.Y = 0;
}
}
LayoutUpdated 经常被调用,因此您可能需要检查 contentRoot.ActualWidth 和 ActualHeight 是否已更改,以查看您是否真的需要清除转换。
关于c# - Silverlight:将子窗口重新定位到中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7326692/