我的WPF程序中有一个关闭(和最小化)按钮。我使用2行来创建关闭按钮的'X'外观。这绝对没问题。
目前,它的工作方式是我有一个Grid父级,两条线和一个透明按钮作为子级。线条的颜色是黄色(十六进制#14FF1A),正如我之前所说,透明背景。当用户的鼠标悬停在按钮上时,我希望背景变为黄色,两条线变成黑色。我已成功将按钮的背景变为黄色,但无法将线条变为黑色。
我正在使用样式,这是我的按钮样式代码。
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
<ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsDefaulted" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background2}"/>
<Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="Black"/>
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background2}"/>
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/>
<Setter Property="BorderThickness" TargetName="border" Value="0,0,0,0"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.Background2}"/>
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.Border}"/>
<Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这是我已经尝试过的(使用数据触发器绑定到按钮的'IsMouseOver')。
<Style x:Key="LineStyle1" TargetType="{x:Type Line}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=parent, Path=IsMouseOver}" Value="True">
<Setter Property="Stroke" Value="Black"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=parent, Path=IsMouseOver}" Value="False">
<Setter Property="Stroke" Value="#14FF1A"/>
</DataTrigger>
</Style.Triggers>
</Style>
此致