首页 > 解决方案 > 来自 ItemsControl 的 Panel.ZIndex 在 WPF .NET Core 应用程序中的 Canvas 上不起作用

问题描述

我在用户控件中有一个画布。在画布内,我有一个 Image 和一个 ItemsControl。例如: <ItemsControl ... /> 项目控件如下所示:

 <ItemsControl 
            x:Name="itmZoneShapes"
            ItemsSource="{Binding LastSelectedZone.PredefinedSelectedLogicalZones}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemContainerStyle>
                <Style TargetType="ContentPresenter">
                    <Setter Property="Canvas.Left" Value="{Binding Path=RectangleCanvasLeft}" />
                    <Setter Property="Canvas.Top" Value="{Binding Path=RectangleCanvasTop}" />
                    <Setter Property="Panel.ZIndex" Value="{Binding Path=RectangleCanvasZIndex}" />
                </Style>
            </ItemsControl.ItemContainerStyle>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Rectangle
                        StrokeThickness="2"
                        Stroke="Black"
                        Tag="{Binding Path=RectangleName}"
                        Width="{Binding Path=RectangleWidth}"
                        Height="{Binding Path=RectangleHeight}"
                        RadiusX="{Binding Path=RectangleRadiusX}"
                        RadiusY="{Binding Path=RectangleRadiusY}">
                        <Rectangle.Fill>
                            <SolidColorBrush 
                                Color="{Binding Path=RectangleBackgroundColor, 
                                UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"
                                Opacity="0.4"/>
                        </Rectangle.Fill>
                        <Rectangle.Clip>
                            <RectangleGeometry Rect="{Binding Path=RectangleClip}"/>
                        </Rectangle.Clip>
                    </Rectangle>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

问题是,绑定的 Panel.ZIndex 以某种方式仅在 ItemsPanelTemplate 标记的 Canvas 中起作用。

我想要的是能够为 itemscontrol 内的每个元素设置 Panel.ZIndex 并在图像上方或图像下方显示矩形(基于 Panel.ZIndex )。现在,我只能更改 ItemsControl Panel.ZIndex,但问题是数据模板中的所有项目都将具有相同的 ZIndex。

是否有可能为 ItemsControl 中的每个项目绑定 Panel.ZIndex 并在 UI 中正确可见?

标签: c#wpfdata-bindingz-indexitemscontrol

解决方案


推荐阅读