首页 > 解决方案 > 有没有办法在同一个画布内设置不同 ItemsControl 的 ZIndex

问题描述

我正在尝试在画布中设置不同 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 的 ZIndexes?

标签: wpfxamlcanvasz-indexitemscontrol

解决方案


在 Clemens 对我的帖子的回答之后,这是我问题的解决方案。在 Windows.Resources 中。

  <Window.Resources>
        <CollectionViewSource x:Key="Points" Source="{Binding AllPoints.PointsObjects}"/>
        <CollectionViewSource x:Key="Tracks" Source="{Binding AllTracks.TrackObjects}"/>
     

        <CompositeCollection x:Key="CombinedCollection">
            <CollectionContainer Collection="{Binding Source={StaticResource Points}}" />
            <CollectionContainer Collection="{Binding Source={StaticResource Tracks}}" />

        </CompositeCollection>
            
    </Window.Resources>

并在画布内。

<Canvas>
        <ItemsControl ItemsSource="{StaticResource CombinedCollection}">
                    <ItemsControl.ItemContainerStyle>
                        <Style TargetType="{x:Type ContentPresenter}">
                            <Setter Property="Panel.ZIndex" Value="{Binding ZIndex}"/>
                        </Style>
                    </ItemsControl.ItemContainerStyle>
                </ItemsControl>
</Canvas>

推荐阅读