在显示子窗口的自定义控件中,我想将 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 第一次被调用为零。下次高度是以前的内容而不是分配的新内容。
如何获取内容展示者的实际身高?有什么活动吗?