如何相对于屏幕将DataTemplate中的Popup居中对齐?

时间:2018-10-25 03:06:15

标签: c# xaml uwp

我在DataTemplate里面有一个弹出窗口,我想要的是居中对齐弹出窗口。屏幕尺寸。下面是我用于使弹出窗口居中的代码,但是这种情况是我在网格内有三行,而ListView在第二行中,这是因为弹出窗口无法容纳屏幕中心。另外,我不能将其移出DateTemplate,因为它需要该项的单独DataContext。

private void Popup_SizeChanged(object sender, SizeChangedEventArgs e) 
{
    var popup = sender as Popup;
    CoreWindow yourContainingWindow = Window.Current.CoreWindow;
    Rect windowBounds = yourContainingWindow.Bounds;
    Point windowCenter = new Point(windowBounds.Left + (windowBounds.Width / 2.0), (windowBounds.Height / 2.0));
    popup.SetValue(Popup.HorizontalOffsetProperty, windowCenter.X - (popup.ActualWidth / 2.0));
    popup.SetValue(Popup.VerticalOffsetProperty, -(popup.ActualHeight / 2.0));
}

关于如何实现上述输出的任何建议?

0 个答案:

没有答案