2011-03-28 114 views
1

我正在使用ChildWindow(Silverlight),它也包含一些擴展器控件。在一種情況下,當擴展器控件擴展時,子窗口的底部向下擴展到底部的屏幕,但仍然在頂部留下空間。Silverlight ChildWindow不能正確重新定位

如何重新定位子窗口以將其居中在屏幕中,就好像我剛打開子窗口一樣? (這很容易,但我不認爲可行)

(手動干預) 我已經經歷了ContentRoot的RenderTransform,並且我在該集合中有六個轉換,其中兩個是TranslateTransforms。如果我更新第一個的X/Y屬性(不知道應該更改哪兩個),並用整個TransformGroup更新RenderTransform屬性,我已經成功地在屏幕上移動ChildWindow - 但它不是按照我的期望行事。

我也不知道爲什麼當擴展器控件展開時,ChildWindow_SizeChanged事件不會觸發。窗戶的大小在變化,爲什麼它不會着火?

好了 - 太多的問題,只需要第一個回答,其餘的都是以填補我的WPF/Silverlight是如何工作的知識......

問候, 理查德通過這個

回答

3

回答博客: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; 

} 

}