Reputation: 6811
I have a canvas containing a number of rectangles like so:
<Canvas x:Name="canvas">
<ItemsControl ItemsSource="{Binding Rectangles}" Canvas.ZIndex="5" Name="RectanglesControl">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Panel.ZIndex" Value="1" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type Rectangle}">
<Button Command="{Binding Command}"
IsEnabled="{Binding IsEnabled}">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Path Data="{Binding Geometry}"
Fill="{Binding FillBrush}"
IsHitTestVisible="True"
Cursor="Hand"
Opacity="{Binding IsFillVisible, Converter={StaticResource BoolToDoubleConverter}}"/>
</ControlTemplate>
</Button.Template>
</Button>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
</Canvas>
A button is created for each rectangle, and the buttons are 'disguised' as rectangles using the Button Template. This is in order to make the rectangles clickable.
Now the issue is that some of the buttons are enabled, and some are not. As a result if a nonenabled button is on top of an enabled button, one cant click on the enabled button.
Is there any way to move enabled buttons to the front, or otherwise allow to click through the non enabled buttons to the enabled ones?
Upvotes: 0
Views: 361
Reputation: 128062
Use a DataTrigger to set the ZIndex property:
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Style.Triggers>
<DataTrigger Binding="{Binding IsEnabled}" Value="True">
<Setter Property="Panel.ZIndex" Value="1"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ItemsControl.ItemContainerStyle>
Upvotes: 1