在画布上为矩形设置zindex不会将其置于前面 [英] Setting zindex for rectangle on canvas is not bringing it to front

查看:69
本文介绍了在画布上为矩形设置zindex不会将其置于前面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在画布上涂有矩形和矩形时遇到了问题。
他们正在以相反的创建顺序获取事件(最新显示在顶部),而不是zindex的顺序...

I've got a problem with canvas and rectangles painted on it. They are gaining events in reversed order of creation (newest is on top), not the order of zindex...

我绑定了ItemsControl资源列表。

I've got ItemsControl binded with list of resources.

然后有一个画布作为项目面板:

Then there is a canvas as item panel:

<ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <Canvas x:Name="BitmapCanvas"/>
    </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>

所有资源都绑定为矩形:

All resources are binded as rectangles:

<ItemsControl.ItemTemplate>
    <DataTemplate DataType="interfaces:IResourceView">
        <Rectangle ...>

有一种样式:

<Rectangle.Style>
    <Style TargetType="{x:Type Rectangle}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=IsSelected}" Value="true">
                <Setter Property="Canvas.ZIndex" Value="0"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=IsSelected}" Value="false">
                <Setter Property="Canvas.ZIndex" Value="15"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
    ...</Rectangle.Style></Rectangle></DataTemplate></ItemsControl.ItemTemplate>

如您所见,选中矩形后,我将其Zindex设置为0,然后其他人则具有更大的zindex值。我也在尝试使用交换值,但是
矩形仍以相同的方式获得关注。
有人知道为什么会这样吗?

As you can see, when the rectangle is selected, I'm setting its Zindex to 0, and others have then zindex value bigger. I was trying it also with swapped values, but still rectangles are gaining focus in the same way. Have anybody got an idea why it is happening like this?

推荐答案

设置 Canvas。 ZIndex (或实际上 DataTemplate中矩形上的WPF中的Panel.ZIndex )无效,因为这些矩形不是ItemsPanelTemplate中Canvas的直接子代。换句话说,矩形不是同级,但是 ZIndex 是一个相对值,仅影响相同容器控件的同级。

Setting Canvas.ZIndex (or actually Panel.ZIndex in WPF) on the Rectangle in the DataTemplate has no effect, since those Rectangles are no direct children of the Canvas in the ItemsPanelTemplate. In other words, the Rectangles are no siblings, but ZIndex is a relative value the only affects the siblings of the same container control.

实际上,每个矩形都放入ContentPresenter的Content(这是ItemsControl的项目容器类型)中。然后将这些ContentPresenters放入画布中。

Actually each rectangle is put into the Content of a ContentPresenter (which is the item container type of an ItemsControl). These ContentPresenters are then put into the Canvas.

要使事情正常进行,您可以将DataTriggers移至 ItemContainerStyle

To get things working, you may move the DataTriggers to the ItemContainerStyle:

<ItemsControl.ItemContainerStyle>
    <Style TargetType="ContentPresenter">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=IsSelected}" Value="true">
                <Setter Property="Panel.ZIndex" Value="0"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=IsSelected}" Value="false">
                <Setter Property="Panel.ZIndex" Value="15"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ItemsControl.ItemContainerStyle>

这篇关于在画布上为矩形设置zindex不会将其置于前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆