Search code examples
wpfxamlcanvasz-indexitemscontrol

Is there a way to set the ZIndex of different ItemsControl within the same canvas


I'm trying to set the ZIndex of different ItemsControls within a canvas and initially I did this which worked fine. Each of the following Collections was organised as expected.

 <ItemsControl ItemsSource="{Binding AllPoints.PointsObjects}" Panel.ZIndex={Binding ZIndex}>

 <ItemsControl ItemsSource="{Binding AllTracks.TrackObjects}" Panel.ZIndex={Binding ZIndex}>

However I need to be able to change the ZIndexes of each item within each ItemsControl individually. So I moved the ZIndex Binding to each individual object and tried to set the ZIndex using ItemsControl.ItemContainerStyle. But the collections did not appear in the order as expected.

                <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>
               

Would I need to create just one ItemsControl or is it possible to link different ItemsControl's ZIndexes by use ItemsControl.ItemContainerStyle?


Solution

  • Following Clemens answer to my post this is the solution to my question. Within the 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>
    

    And within the Canvas.

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