我正在尝试创建一个简单的按钮模板,其中按钮通常看起来像一条水平线,但是当鼠标悬停在按钮上时,按钮后面会显示一个“矩形”颜色填充。这是我的代码,但我似乎无法触发触发器。
<Window x:Class="TestStylingWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<WindowChrome.WindowChrome>
<WindowChrome CaptionHeight="36" GlassFrameThickness="0 0 0 1" ResizeBorderThickness="5" />
</WindowChrome.WindowChrome>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="36" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Height="36" Width="36"
HorizontalAlignment="Center" VerticalAlignment="Center"
Grid.Row="0">
<Button.Template>
<ControlTemplate>
<Grid>
<Rectangle x:Name="PART_ButtonBackgroundRectangle" Fill="LightGray" Width="36" Height="36" Opacity="0" />
<Path x:Name="PART_ButtonPath" Data="M10,26 L26,26" Stroke="DarkGray" StrokeThickness="1.5" />
</Grid>
<ControlTemplate.Triggers>
<Trigger SourceName="PART_ButtonBackgroundRectangle" Property="IsMouseOver" Value="True">
<Setter TargetName="PART_ButtonBackgroundRectangle" Property="Opacity" Value="1" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
</Window>
有谁知道为什么我的触发器不起作用,或者也许有更好的方法来做到这一点?我对 WPF 有点陌生,想重构它(以便在许多按钮中使用它),但我不确定如何重构。
Update:好的,所以我想这似乎是因为该按钮位于窗口的标题栏中。有没有好的办法解决这个问题?也许有一种方法可以设置按钮的点击/悬停优先级?
您需要设置按钮的 Hittest 可见,例如:
shell:WindowChrome.IsHitTestVisibleInChrome="True"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)