Setting zindex for rectangle on canvas is not brin

2019-01-20 14:20发布

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

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

and there is a style:

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

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?

2条回答
可以哭但决不认输i
2楼-- · 2019-01-20 15:04

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.

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.

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>
查看更多
在下西门庆
3楼-- · 2019-01-20 15:18

You issue is with your Triggers. Style only supports EventTrigger, not any of the other types so your Triggers will never be executed. You are in a DataTemplate though, and those do support DataTrigger so you can move some things around to fix the issue:

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <Rectangle x:Name="Rect"/>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=IsSelected}" Value="true">
                <Setter TargetName="Rect" Property="Canvas.ZIndex" Value="0"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=IsSelected}" Value="false">
                <Setter TargetName="Rect" Property="Canvas.ZIndex" Value="15"/>
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</ItemsControl.ItemTemplate>
查看更多
登录 后发表回答