C# 如何在Windows 10 MapControl中向XAML控件添加Z索引
1在几层中添加图钉和任何你喜欢的东西都很容易,清除这些层,你可以确保某些项目在其他项目前面。在Windows 10中,该控件消失了,取而代之的是一个控件,它拥有我真正需要的东西的一半,但在3D和场景中(我不使用的东西) 我的第一次尝试是,好的,MapPolygon具有ZIndex,因为它继承自MapElement并实现C# 如何在Windows 10 MapControl中向XAML控件添加Z索引,c#,xaml,win-universal-app,bing-maps,uwp-maps,C#,Xaml,Win Universal App,Bing Maps,Uwp Maps,1在几层中添加图钉和任何你喜欢的东西都很容易,清除这些层,你可以确保某些项目在其他项目前面。在Windows 10中,该控件消失了,取而代之的是一个控件,它拥有我真正需要的东西的一半,但在3D和场景中(我不使用的东西) 我的第一次尝试是,好的,MapPolygon具有ZIndex,因为它继承自MapElement并实现 internal interface IMapElement { System.Boolean Visible { get; set; } System.Int3
internal interface IMapElement
{
System.Boolean Visible { get; set; }
System.Int32 ZIndex { get; set; }
}
很好,让我们在我的UserControl中实现它,这样map控件就知道如何绘制每个元素。惊喜这个界面是内部的,你只能盯着它看
第二,也许他们用画布来画元素,画布上有一个ZIndex,好吧,不用说它不起作用
pin.SetValue(Canvas.ZIndexProperty)代码>
有谁能告诉我UWP MapControl中是否有类似的图层,或者是否有方法告诉MapControl在哪个索引中绘制项目
问候。您不能声明这样做吗?考虑下面的XAML:
<maps:MapControl x:Name="myMap" HorizontalAlignment="Left">
<maps:MapItemsControl x:Name="MapItems" ItemsSource="{Binding}">
<maps:MapItemsControl.ItemTemplate>
<DataTemplate>
<Button x:Name="MapItemButton" Background="Transparent">
<StackPanel>
<Border Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Text="{Binding Title}"/>
</Border>
<Image Source="{Binding ImageSourceUri}" maps:MapControl.Location="{Binding Location}">
<Image.Transitions>
<TransitionCollection>
<EntranceThemeTransition/>
</TransitionCollection>
</Image.Transitions>
</Image>
</StackPanel>
</Button>
</DataTemplate>
</maps:MapItemsControl.ItemTemplate>
</maps:MapItemsControl>
</maps:MapControl>
在这里,我基本上是使用常规XAML功能将图像(mapelements)直接绑定到地图的位置。使用网格层、StackPanel和诸如此类的工具来控制上面的内容、下面的内容等。当然,你也可以通过codebehind来构建它。我不确定它是否解决了你的问题,但肯定它解决了我的问题,也可以帮助其他人
public class MapControl : DependencyObject
{
#region Dependency properties
public static readonly DependencyProperty ZIndexProperty = DependencyProperty.RegisterAttached("ZIndex", typeof(int), typeof(MapControl), new PropertyMetadata(0, OnZIndexChanged));
#endregion
#region Methods
public static int GetZIndex(DependencyObject obj)
{
return (int)obj.GetValue(ZIndexProperty);
}
private static void OnZIndexChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ContentControl us = d as ContentControl;
if (us != null)
{
// returns null if it is not loaded yet
// returns 'DependencyObject' of type 'MapOverlayPresenter'
var parent = VisualTreeHelper.GetParent(us);
if (parent != null)
{
parent.SetValue(Canvas.ZIndexProperty, e.NewValue);
}
}
}
public static void SetZIndex(DependencyObject obj, int value)
{
obj.SetValue(ZIndexProperty, value);
}
#endregion
}
名称空间
xmlns:maps="using:Windows.UI.Xaml.Controls.Maps"
xmlns:mapHelper="using:yournamespace"
控制
<maps:MapControl>
<maps:MapItemsControl ItemsSource="...">
<maps:MapItemsControl.ItemTemplate>
<DataTemplate x:DataType="...">
<YourTemplate mapHelper:MapControl.ZIndex="{x:Bind ZIndex, Mode=OneWay}" />
</DataTemplate>
</maps:MapItemsControl.ItemTemplate>
</maps:MapItemsControl>
</maps:MapControl>
我知道我现在可以将datatemplate应用于控件,但我想要的是,如果我有50个元素,并且如果我单击第25个元素,则可以将其放在前面轻松更改ZIndex属性。不,我没有。你只需要做一些技巧,比如在排序后以某种方式添加元素。我也遇到了同样的问题,我按照你的答案解决了这个问题,但做了一些修改。在我的例子中,YourTemplate
是一个StackPanel
,因此为了使上述代码正常工作,我必须将ContentControl us=d作为ContentControl删除因为它总是将其强制转换为null。因此,我删除了外部的if语句,一切都按预期工作。非常感谢。