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?
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, butZIndexis 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: