【发布时间】:2021-07-26 15:32:21
【问题描述】:
我正在尝试在画布中设置不同 ItemsControls 的 ZIndex,最初我这样做效果很好。以下每个集合都按预期组织。
<ItemsControl ItemsSource="{Binding AllPoints.PointsObjects}" Panel.ZIndex={Binding ZIndex}>
<ItemsControl ItemsSource="{Binding AllTracks.TrackObjects}" Panel.ZIndex={Binding ZIndex}>
但是,我需要能够单独更改每个 ItemsControl 中每个项目的 ZIndexes。所以我将 ZIndex 绑定移动到每个单独的对象,并尝试使用 ItemsControl.ItemContainerStyle 设置 ZIndex。但是集合并没有按预期的顺序出现。
<ItemsControl ItemsSource="{Binding AllPoints.PointsObjects}">
<ItemsControl.Resources>
<!--Resource dictionary template-->
</ItemsControl.Resources>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="Canvas.ZIndex" Value="{Binding ZIndex}"/>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
<ItemsControl ItemsSource="{Binding AllTracks.TrackObjects}">
<ItemsControl.Resources>
<!--Resource dictionary template-->
</ItemsControl.Resources>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="Canvas.ZIndex" Value="{Binding ZIndex}"/>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
我需要只创建一个 ItemsControl 还是可以通过使用 ItemsControl.ItemContainerStyle 来链接不同 ItemsControl 的 ZIndex?
【问题讨论】:
-
ZIndex 仅对单个 Panel 的子级有意义。您需要使用单个 ItemsControl,并且可能将 PointObjects 和 TrackObjects 放入 CompositeCollection。
-
ZIndex 是面板的可附加属性。它本质上不是全局的,而是仅由元素所在的一个面板处理。在您的情况下,ItemsControl 项目的 ZIndex 会影响它们在 ItemsPanel 中的顺序。而 ItemsPanel 本身的 ZIndex 会影响 ItemsPanel 之间的顺序。如果一个 ItemsPanel 的索引低于另一个,那么它的所有项目都将低于另一个项目,而不管项目索引如何。
-
如果您需要在元素之间进行全局洗牌,则必须将所有元素组合到一个集合中,以便它们都出现在一个面板中。
标签: wpf xaml canvas z-index itemscontrol