【发布时间】:2014-04-18 08:20:09
【问题描述】:
这个问题有很多不同的答案,但没有找到适用于 Windows Phone 的问题。
所以这里是:
我有一个显示用户控件的项目控件。
我像这样将 ObservableCollection 绑定到这个 ItemsControl:
<Canvas Name="CanvasGrid" Grid.Column="1" Background="Transparent" Canvas.ZIndex="5">
<ItemsControl ItemsSource="{Binding InGame}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Canvas>
<View:SInGame/>
</Canvas>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Canvas>
当一个元素被添加到 Itemscontrol 中时,该元素会被赋予一个 zindex,这与我赋予该元素的内容以及显示的用户控件无关。我想要做的是拥有可以在 z 方向上上下移动所选元素的按钮。
查看 SInGame
itemscontrol 中的 Canvas 需要从以下视图正确绑定位置:
<UserControl x:Class="MVVMTestApp.View.SInGame"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP8"
xmlns:View="clr-namespace:MVVMTestApp.View"
mc:Ignorable="d"
Canvas.Left="{Binding pos.x}" Canvas.Top="{Binding pos.y}">
<Path >
<.....
....../>
</Path>
</UserControl>
然后可以说视图应该由一个绑定了位置的 Canvas 组成。但这仍然会留下 zindex 的问题。
如 cmets 中所述。 ItemsControl 看不到外面的画布,因此不能使用 zindex 来达到它。那么如何设置zindex呢?还是不能使用ItemsControl 元素?
【问题讨论】:
-
移除 ItemsControl 的 DataTemplate 中的 Canvas。拥有另一个画布会混淆 ZIndex 的渲染。之后,我相信您可以将 ZIndex 绑定到您的实体的属性(InGame 集合中的项目)
-
@ShawnKendrot 没有画布元素不会被渲染,所以这是不可能的?
-
当然会渲染。 ItemsControl 不需要 Canvas 位于 DataTemplate 中
-
@ShawnKendrot 对不起,我记错了为什么画布在那里。我更新了问题。但问题是 itemscontrol 无法看到外围画布,因此视图中的位置绑定将无法到达画布。
标签: c# xaml windows-phone-8 observablecollection itemscontrol