【问题标题】:Set mapcontrol's children topmost将 mapcontrol 的子项设置在最顶层
【发布时间】:2017-08-21 06:33:45
【问题描述】:

我正在向地图控件添加一个堆栈面板。如下所示

但是之前添加的一些点在我的堆栈面板的顶部。如何将我的堆栈面板设置在最顶层?

XAML:

<Grid x:Name="gridMain">
        <maps:MapControl
            x:Name="mapControl"
            ZoomInteractionMode="GestureAndControl"
            TiltInteractionMode="GestureAndControl"
            RotateInteractionMode="GestureAndControl">
            <!--ZoomLevel="{x:Bind ViewModel.ZoomLevel, Mode=OneWay}"
            Center="{x:Bind ViewModel.Center, Mode=OneWay}"-->


            <maps:MapItemsControl x:Name="MapItems">
                <maps:MapItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid Tapped="MagPoint_Tapped" maps:MapControl.NormalizedAnchorPoint="{Binding NormalizedAnchorPoint}" maps:MapControl.Location="{Binding Location}">
                            <Ellipse Canvas.ZIndex="0" Width="{Binding Mag5}" Height="{Binding Mag5}" Fill="{Binding MagColor}"/>
                            <!--<TextBlock Text="{Binding Mag}"/>-->
                        </Grid>

                    </DataTemplate>
                </maps:MapItemsControl.ItemTemplate>
            </maps:MapItemsControl>
        </maps:MapControl>
    </Grid>

并添加面板代码。

StackPanel sp = new StackPanel();
            sp.Background = new SolidColorBrush(Colors.White);
            sp.CornerRadius = new CornerRadius(15);
            sp.BorderBrush = new SolidColorBrush(Colors.LightGray);
            sp.BorderThickness = new Thickness(1);
            sp.Width = 260;
            sp.MinHeight = 180;
            sp.Padding = new Thickness(10);
            Canvas.SetZIndex(sp, 99999);

mapControl.Children.Add(sp);
            Windows.UI.Xaml.Controls.Maps.MapControl.SetLocation(sp, new Geopoint(new BasicGeoposition { Longitude = (double)fi.geometry.coordinates[0], Latitude = (double)fi.geometry.coordinates[1] }));
            Windows.UI.Xaml.Controls.Maps.MapControl.SetNormalizedAnchorPoint(sp, new Point(0.5, 1));

【问题讨论】:

    标签: c# uwp uwp-xaml


    【解决方案1】:

    您设置ZIndex 的方式不起作用,因为StackPanelMapItemsControl 中的项目位于不同的主机中。

    Live Visual Tree 的帮助下,您可以了解它们的布局方式。

    在上面的屏幕截图中,StackPanel 的主机(即第一个 Canvas)被放置在 后面MapOverlayPresenters 的主机(即第二个 Canvas 其中MapItemsControl已插入)。因此,为了让StackPanel 位于它们之上,您需要手动将第一个 CanvasZIndex 设置为1

    一旦你明白了这一点,解决方案就变得简单了——

    Loaded += (s, e) =>
    {
        // GetChildByName comes from
        // https://github.com/JustinXinLiu/Continuity/blob/0cc3d7556c747a060d40bae089b80eb845da84fa/Continuity/Extensions/UtilExtensions.cs#L44
        var layerGrid = mapControl.GetChildByName<Grid>("LayerGrid");
        var canvas1 = layerGrid.Children.First();
    
        Canvas.SetZIndex(canvas1, 1);
    };
    

    希望这会有所帮助!

    【讨论】:

    • 谢谢,贾斯汀。我发现你什么都知道,哈哈:)而且我这次也学会了实时视觉树。
    • 哈哈谢谢我受宠若惊。 :P 是的,LVT 非常有用,我一直都在使用它!
    猜你喜欢
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    相关资源
    最近更新 更多