2013-10-22 94 views
0

我想使按鈕中的文本變爲KeyboardFocused時發光。如何將BitmapEffect添加到樣式化按鈕的文本

我無法做到這一點。這裏是我的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> 

非常感謝!無法找到我的問題在谷歌/ SO的解決方案。

回答

2

我還沒有使用您的確切模板,因爲我無法在不更改資源名稱的情況下將其複製/粘貼到Visual Studio中。在發佈修復代碼並使其完全隔離時,使修復問題對我們來說更加容易:D

我設置了鍵盤焦點時,我在下面提供的模板將陰影效果應用於按鈕的內容,我使用效果而不是BitMapEffect,因爲它速度更快,而且BitMapEffect現在已經過時。您可能需要調整效果參數才能使其發光更好。

enter image description here

<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> 
+0

完美地工作!感謝您的解決方案並提示BitMapEffect。另外,我很抱歉張貼了很多我自己的資源引用。下次嘗試更清楚。乾杯:) – Rachael

相關問題