0

我想让按钮内的文本在变为 KeyboardFocused 时发光。

我无法做到这一点。这是我的xml:

 <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 上找到我的问题的解决方案。

4

1 回答 1

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>
于 2013-10-22T22:22:15.497 回答