WPF contentpresenter高度

时间:2013-06-07 13:57:52

标签: wpf height contentpresenter

在显示子窗口的自定义控件中,我想设置Canvas内部网格的左侧和顶部显示在父控件的中心。

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);
} 

但是第一次调用ActualHeight时它会变为零。此外,下一次高度是先前的内容而不是分配的新内容。

如何获取内容演示者的实际高度?有什么事吗?

0 个答案:

没有答案