2011-03-28 4 views
1

J'utilise ChildWindow (Silverlight) qui contient également des contrôles d'extension. Dans un cas, lorsque le contrôle de l'extension se développe, le bas de la fenêtre enfant se déploie vers le bas sur l'écran en bas, mais laisse toujours de la place en haut.Silverlight ChildWindow ne repositionne pas correctement

Comment puis-je repositionner une fenêtre enfant pour la centrer sur l'écran, comme si je venais d'ouvrir la fenêtre enfant? (Ce serait facile, mais je ne pense pas faisable)

(intervention manuelle) Je suis passé par RenderTransform de ContentRoot, et j'ai six transformations dans cette collection, dont 2 sont TranslateTransforms. Si je mets à jour les propriétés X/Y du premier (je ne sais pas lequel des deux je devrais changer) ET met à jour la propriété RenderTransform avec l'ensemble TransformGroup, j'ai réussi à déplacer la ChildWindow autour de l'écran - mais ce n'est pas se comporter comme je m'y attends. Je ne sais pas non plus pourquoi l'événement ChildWindow_SizeChanged ne se déclenche pas lorsque le contrôle Expander se développe. La fenêtre change de taille, alors pourquoi ne tire-t-elle pas?

Ok - trop de questions, juste besoin la première réponse, le reste est de remplir ma connaissance de la façon dont WPF/Silverlight fonctionne ...

Cordialement, Richard

Répondre

3

réponse via cette blog: 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; 

} 

}

Questions connexes