当它变成KeyboardFocused时,我想让按钮发光。
我无法做到这一点。这是我的xaml:
<Style x:Key="BorderlessSymbolButtonStyle" TargetType="{x:Type Button}" >
<Setter Property="FocusVisualStyle" Value="{DynamicResource MetroButtonFocusVisual}"/>
<Setter Property="BitmapEffect">
<Setter.Value>
<OuterGlowBitmapEffect GlowColor="White" Opacity="0" GlowSize="3" Noise=".1"/>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid x:Name="Grid">
<Border x:Name="Border" Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"/>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
x:Name="Content"
Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
RecognizesAccessKey="True"
BitmapEffect="{TemplateBinding BitmapEffect}" >
</ContentPresenter>
</Grid>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsPressed" Value="true"/>
<Condition Property="IsEnabled" Value="true"/>
</MultiTrigger.Conditions>
<Setter Property="Foreground" Value="{DynamicResource GlenairMediumBlue}"/>
</MultiTrigger>
<Trigger Property="IsKeyboardFocused" Value="true">
<!--<Setter Property="BorderBrush" Value="{DynamicResource DefaultedBorderBrush}" TargetName="Border"/>-->
<!--<Setter Property="Effect" TargetName="Border" Value="{DynamicResource BorderDropShadow}"/>-->
<Setter Property="BitmapEffect" >
<Setter.Value>
<OuterGlowBitmapEffect GlowColor="White" Opacity=".4" GlowSize="3" Noise=".1"/>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="Gray"/>
</Trigger>
<!--<Trigger Property="IsEnabled" Value="true">
<Setter Property="Foreground" Value="White"/>
</Trigger>-->
<!--<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="{DynamicResource MouseOverBrush}" TargetName="Border"/>
</Trigger>-->
<!--<Trigger Property="IsPressed" Value="true">
<Setter Property="Foreground" Value="{DynamicResource GlenairLighterOpaqueBlueSolidColorBrush}" />
</Trigger>-->
<!--<Trigger Property="IsEnabled" Value="true"/>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" Value="{DynamicResource DisabledBackgroundBrush}" TargetName="Border"/>
<Setter Property="BorderBrush" Value="{DynamicResource DisabledBorderBrush}" TargetName="Border"/>
<Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}"/>
</Trigger>-->
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
非常感谢!无法在google / SO上找到我的问题的解决方案。
答案 0 :(得分:2)
我没有使用您的确切模板,因为我无法在不更改资源名称的情况下将其复制/粘贴到Visual Studio中。发布代码以修复尝试并使其完全隔离时,使我们更容易解决问题:D
我在下面提供的模板在设置键盘焦点时对按钮的内容应用了阴影效果,我使用了Effect而不是BitMapEffect,因为它更快,而且BitMapEffect现在已经过时了。您可能需要调整效果参数以使其发光更好。
<Window x:Class="GlowingButton.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="WhiteSmoke"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border BorderBrush="Blue" CornerRadius="5" BorderThickness="1">
<ContentPresenter x:Name="test" HorizontalAlignment="Center" VerticalAlignment="Center" Content="{TemplateBinding Content}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused" Value="True">
<Trigger.Setters>
<Setter TargetName="test" Property="Effect">
<Setter.Value>
<DropShadowEffect Color="Blue" BlurRadius="10" ShadowDepth="0"/>
</Setter.Value>
</Setter>
</Trigger.Setters>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button Margin="100" Content="test" Foreground="Black" FontSize="20"/>
</Grid>
</Window>