c# - WPF 将启用按钮移到前面
问题描述
我有一个包含许多矩形的画布,如下所示:
<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>
为每个矩形创建一个按钮,并使用按钮模板将这些按钮“伪装”为矩形。这是为了使矩形可点击。
现在的问题是某些按钮已启用,而有些则未启用。结果,如果未启用的按钮位于已启用的按钮之上,则无法单击已启用的按钮。
有没有办法将启用的按钮移到前面,或者允许通过未启用的按钮单击启用的按钮?
解决方案
使用 DataTrigger 设置 ZIndex 属性:
<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>
推荐阅读
- powershell - Copy Recently Modified Files with Folder Structure Using PowerShell
- sql-server - 如何在使用 Power Bi 报表服务器(本地)时发送数据警报
- ios - WebStorm:为 React Native 设置模拟器 ID
- bootloader - 是否有像 rEFInd 这样的图形引导加载程序,但适用于具有 BIOS(不是 UEFI)的计算机?
- php - 如何在导航栏中显示我的名字和姓氏的值
- netbeans - 我无法使用 NetBeans 看到我的数据库 Informix 中的表,并且我无法执行所有 SQL 语句
- java - 包:我的包中缺少配置、控制器、dao、实体、表单、模型、实用程序...
- apache-kafka-streams - 如何将从一个主题创建的流加入到从另一个主题派生的KTable(作为聚合操作)
- machine-learning - sklearn中的GridSearchCV如何选择交叉验证集?
- sql - CASE WHEN 中的计算