Silverlight 4.0 Silverlight儿童窗口未正确重新定位
我正在使用ChildWindow(Silverlight),它还包含一些扩展控件。在一种情况下,当expander控件展开时,子窗口的底部向下展开,超出底部的屏幕,但仍然在顶部留下空间 如何重新定位子窗口以使其在屏幕中居中,就好像我刚刚打开了子窗口一样?(这很容易,但我认为不可行) (人工干预) 我已经完成了ContentRoot的RenderTransform,在该集合中有六个转换,其中两个是TranslateTransforms。如果我更新第一个的X/Y属性(不知道应该更改两个属性中的哪一个),并用整个TransformGroup更新RenderTransform属性,我就成功地在屏幕上移动了ChildWindow,但它的行为并不像我预期的那样 我也不知道当Expander控件扩展时,为什么ChildWindow\u SizeChanged事件不触发。窗户大小不一样,为什么不点火呢 好的-问题太多了,只需要回答第一个问题,其余的都是为了补充我对WPF/Silverlight如何工作的了解 问候,, Richard通过本博客回答:Silverlight 4.0 Silverlight儿童窗口未正确重新定位,silverlight-4.0,childwindow,Silverlight 4.0,Childwindow,我正在使用ChildWindow(Silverlight),它还包含一些扩展控件。在一种情况下,当expander控件展开时,子窗口的底部向下展开,超出底部的屏幕,但仍然在顶部留下空间 如何重新定位子窗口以使其在屏幕中居中,就好像我刚刚打开了子窗口一样?(这很容易,但我认为不可行) (人工干预) 我已经完成了ContentRoot的RenderTransform,在该集合中有六个转换,其中两个是TranslateTransforms。如果我更新第一个的X/Y属性(不知道应该更改两个属性中的哪一个
//
///将Silverlight儿童窗口置于屏幕中央。
///
///
///1)Visual TreeHelper将抓取子窗口中的第一个元素-这是Chrome(标题栏、关闭按钮等)
///2)ContentRoot-是该Chrome中用于ChildWindow的第一个元素-在控件模板(ChildWindow)中命名为
///3)使用容器(名为ContentRoot),拉出所有移动或更改控件布局的“变换”
///TranslateTransform-提供控件应放置位置的X、Y坐标
///4)使用Linq表达式,从TransformGroup中获取最后一个TransLateTransfrom
///5)将TranslateTransform重置为点0,0,该点应将子窗口引用为窗口的左上角。然而,这是一个很好的设置
///可能会被默认行为覆盖,以便始终根据窗口大小和浏览器大小将窗口重置到屏幕中间
///我本想制作动画,但这可能需要一个故事板事件,我现在没有时间。
///
///在WPF中移动或更改窗口的整个过程是一次完整的学习体验。
///
///孩子打开窗户。
公共静态无效中心屏幕(此子窗口子窗口)
{
var root=VisualTreeHelper.GetChild(childWindow,0)作为FrameworkElement;
如果(root==null){return;}
var contentRoot=root.FindName(“contentRoot”)作为框架元素;
如果(contentRoot==null){return;}
var transformgroup=contentRoot.RenderTransform作为transformgroup;
如果(transformgroup==null){return;}
TranslateTransform transform=transformgroup.Children.OfType().LastOrDefault();
如果(transform==null){return;}
变换X=0;
变换Y=0;
}
}
/// <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;
}