0

In a custom control which displays Child window, I want to set the Left and Top of a grid which is inside Canvas to display at the center of parent control.

public void show(object view)
{
    var presenter = Template.FindName("PART_DialogView", this) as ContentPresenter;

    if (presenter == null)
        return;

    IsDialogVisible = true; 
    presenter.Content = view;
    CenterPosition(presenter);
}

private void CenterPosition(ContentPresenter presenter)
{
    var dialog = this.GetTemplateChild("Dialog") as Grid;
    if (presenter.ActualHeight != 0.0 && presenter.ActualWidth != 0.0)
    {
        DialogLeft = (this.ActualWidth - presenter.ActualWidth) / 2.0; 
        DialogTop = (this.ActualHeight - presenter.ActualHeight) / 2.0;
    }
    Canvas.SetLeft(dialog, DialogLeft);
    Canvas.SetTop(dialog, DialogTop);
} 

But the ActualHeight is coming as zero for first time this is called. Also next time the height is of previous content not the new content assigned.

How to get the actual height of content presenter? Is there any event?

LPL
  • 16,827
  • 6
  • 51
  • 95
Avinash Tiwari
  • 135
  • 1
  • 10
  • http://stackoverflow.com/a/1695821/1834662 try that?? – Viv Jun 07 '13 at 15:14
  • Or [`presenter.UpdateLayout()`](http://msdn.microsoft.com/en-us/library/system.windows.uielement.updatelayout.aspx). – LPL Jun 07 '13 at 15:16

0 Answers0