如何找到膨胀膨胀机的中心

How to find the center of a expanding expander

好吧,这让我很困惑,它应该很简单

我有这个代码

private void Expander_Expanded(object sender, RoutedEventArgs e)
{
    var expand = e.OriginalSource as Expander;
    if (expand != null)
    {
        var layer = sender as LayerBase;
        var middle = expand.TranslatePoint(new Point(), layer) + new Vector(expand.ActualWidth/2,expand.ActualHeight/2);
        Location.Centre = layer.ScreenToGeoPoint(middle);
        e.Handled = true;
    }
}

这就是代码应该做的,

这是为了确保扩展器位于所显示地图的中心,而不需要相对于地图移动扩展器(即左上角仍处于正确的地理位置)

代码本身正在运行,但是,ActualHeight 和 ActualWidth 正在返回折叠扩展器的大小,这大大偏离了中心点,我假设这是因为 Expanded 事件在控制重绘。那么我如何捕捉可视化树重绘后展开的变化?

private async void Expander_Expanded(object sender, RoutedEventArgs e)
{
    var expand = e.OriginalSource as Expander;
    if (expand != null)
    {
        var layer = sender as LayerBase;
        // magic
        await Dispatcher.InvokeAsync( ( ) => { } );
        expand.UpdateLayout();
        var middle = expand.TranslatePoint(new Point(), layer) + new Vector(expand.ActualWidth/2,expand.ActualHeight/2);
        Location.Centre = layer.ScreenToGeoPoint(middle);
        e.Handled = true;
    }
}

这有效:)