Silverlight ZOrder在Bing控件中不适合我

时间:2013-03-16 20:31:37

标签: silverlight xaml bing-maps z-order

我正在尝试让我的弹出窗口小部件在地图中位于顶部,但设置Canvas.ZOrder并没有帮助。

这是XAML:

<m:Map x:Name="MainMap"
            Margin="0,6,3,3"
            ZoomLevel="{Binding MapZoomLevel, Mode=TwoWay}"
            Center="{Binding MapCenter, Mode=TwoWay}"
            CopyrightVisibility="Collapsed"
            CredentialsProvider="{Binding BingCredentialsProvider}"
            UseInertia="True" 
            Mode="Road" Grid.Column="2" Grid.Row="1">
            <m:MapItemsControl ItemsSource="{Binding Source={StaticResource WorkLayerData}}">
                <m:MapItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Canvas
                            m:MapLayer.Position="{Binding Location}">
                            <Button                                
                                Width="{Binding PushpinWidth}" Height="{Binding PushpinWidth}"
                                Margin="{Binding PushpinMargin}"
                                Style="{StaticResource LooklessButtonStyle}"
                                Command="{Binding DataContext.SelectedPushpinChangedCommand, ElementName=LayoutRoot}"
                                CommandParameter="{Binding}"
                                Cursor="Hand">
                                <Ellipse
                                    Width="{Binding PushpinWidth}" Height="{Binding PushpinWidth}" Stroke="Black" Fill="{Binding IsGPSDataRecent, Converter={StaticResource BoolToGreenRedBrushConverter}}" StrokeThickness="1">
                                    <ToolTipService.ToolTip>
                                        <TextBlock Text="{Binding DeviceId}" />
                                    </ToolTipService.ToolTip>
                                </Ellipse>
                            </Button>

                            <!-- Show black dot over actual GPS point -->
                            <Ellipse
                                Width="10" Height="10" Stroke="Black" Fill="Black" StrokeThickness="1"
                                Margin="-5,-5,0,0"
                                Visibility="{Binding IsSelected, Converter={StaticResource BoolToVisibilityConverter}}" />

                            <Border
                                Width="200"
                                BorderThickness="1" BorderBrush="DarkGray"
                                Visibility="{Binding IsSelected, Converter={StaticResource BoolToVisibilityConverter}}">
                                <Border.Effect>
                                    <DropShadowEffect BlurRadius="5"  Color="#FF000000" Opacity="0.5" ShadowDepth="2" />
                                </Border.Effect>
                                <ContentControl Template="{StaticResource TrackedAssetControlTemplate}" />
                            </Border>
                        </Canvas>                                               
                    </DataTemplate>
                </m:MapItemsControl.ItemTemplate>
            </m:MapItemsControl>
        </m:Map>

我试图在边框上设置ZIndex,但没有运气。 以下是IsSelected = true时的外观(参见顶部ZIndex较高的其他点)

enter image description here

1 个答案:

答案 0 :(得分:1)

为了将MapItemsControl中的项目置于前面,需要设置项目容器的ZIndex。您可以通过从MapItemsControl的ItemContainerGenerator检索项容器来在代码中执行此操作。

如果您不希望这样,您可以将附加的帮助器属性应用于DataTemplate中的顶级容器(Canvas)以用于您的地图项。由于此Canvas是项容器的直接子项,因此helper属性必须设置Canvas的可视父项的ZIndex。如果这听起来很奇怪,这里是附加属性的代码,在一个名为MapItem的辅助类中:

public class MapItem
{
    public static readonly DependencyProperty ZIndexProperty =
        DependencyProperty.RegisterAttached("ZIndex", typeof(int),
            typeof(MapItem), new PropertyMetadata(ZIndexChanged));

    public static int GetZIndex(DependencyObject obj)
    {
        return (int)obj.GetValue(ZIndexProperty);
    }

    public static void SetZIndex(DependencyObject obj, int value)
    {
        obj.SetValue(ZIndexProperty, value);
    }

    private static void ZIndexChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        // set ZIndex on parent of obj
        Canvas.SetZIndex((UIElement)VisualTreeHelper.GetParent(obj), (int)e.NewValue);
    }
}

在DataTemplate中,您现在可以使用适当的绑定转换器将helper属性绑定到您的VM属性之一:

<DataTemplate x:Key="MapItemDataTemplate">
    <!-- setting the helper property MapItem.ZIndex on Canvas
         sets the Canvas.ZIndex property on the item container -->
    <Canvas local:MapItem.ZIndex="{Binding ...}">
        ...
    </Canvas>
</DataTemplate>