简体   繁体   English

如何找到展开式扩展器的中心

[英]How to find the center of a expanding expander

OK this has me puzzled and it should be simple 好,这让我感到困惑,应该很简单

I have this code 我有这个代码

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

This is what the code is supposed to do, 这就是代码应该做的,

  • Locate the Screen point in the centre of the control 在控件中心找到屏幕点
  • locate the Geographic point that relates to that screen point 找到与该屏幕点相关的地理点
  • recentre the map to those coordinates 将地图重新​​定位到这些坐标

this is so as to ensure the expander is in the centre of the displayed map, with out moving the expander relative to the map (ie so the top left corner is still in the correct geographic location) 这是为了确保扩展器位于显示的地图的中心,而不会相对于地图移动扩展器(即,左上角仍在正确的地理位置)

the code itself is functioning however, ActualHeight and ActualWidth are returning the size of the collapsed expander, which is throwing off the centre point by a large margin, I'm assuming that this because the Expanded event is firing before the control redraws. 代码本身正在运行,但是,ActualHeight和ActualWidth返回折叠的扩展器的大小,该扩展器大幅度偏离中心点,我认为这是因为在控件重绘之前触发了Expanded事件。 so how do I capture that expanded has changed after the visual tree redraws? 那么如何在可视树重绘之后捕获扩展的变化?

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

This works :) 这有效:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM