2016-04-22 32 views
0

ButtonControlTemplate私はIsPressedイベントにアクセスする必要があります。それ、どうやったら出来るの?WPFコントロールテンプレートによるクリック時のButto DropShadowEffectの削除

背景:私はをButtonStyleに追加しましたが、ぼやけたテキストがありました。 This fix solved the blurry text issue.しかし、これは新しい問題を追加しました。ユーザーがボタンをクリックすると、DropShadowEffectを削除したかったのです。私はこれをそうするだろうと考えました。

<Style x:Key="DropShadowButtons" TargetType="Button"> 
<Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="{x:Type Button}"> 
      <Grid> 
       <Border Background="{TemplateBinding Background}"> 
        <Border.Style> 
         <Style TargetType="{x:Type Border}"> 
          <Setter Property="Border.Effect"> 
           <Setter.Value> 
            <DropShadowEffect Color="Black" Opacity="0.5" /> 
           </Setter.Value> 
          </Setter> 
          <Style.Triggers> 
           <Trigger Property="Button.IsPressed" Value="False"> 
            <Setter Property="Border.Effect"> 
             <Setter.Value> 
              <DropShadowEffect Color="Black" Direction="320" ShadowDepth="0" BlurRadius="0" Opacity="0" /> 
             </Setter.Value> 
            </Setter> 
           </Trigger> 
          </Style.Triggers> 
         </Style> 
        </Border.Style> 
       </Border> 
       <Border Background="{TemplateBinding Background}"> 
        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5,2,5,0"/> 
       </Border> 
      </Grid> 
     </ControlTemplate> 
    </Setter.Value> 
</Setter> 

はその後Button.IsPressedBorderの一部で発火しないことを考え出しました。では、ControlTemplateはButtonのIsPressedイベントにどのようにアクセスしますか?

答えて

0

Borderのスタイルではなく、ControlTemplateでトリガーが必要です。セッターがボーダーをターゲットにするためには、ボーダーに名前を付ける必要があります。セッターにターゲット名を与える必要があります:

<ControlTemplate TargetType="{x:Type Button}"> 
    <Grid> 
     <Border x:Name="BackgroundBorder" Background="{TemplateBinding Background}"> 
      <Border.Style> 
       <Style TargetType="{x:Type Border}"> 
        <Setter Property="Border.Effect"> 
         <Setter.Value> 
          <DropShadowEffect Color="Black" Opacity="0.5" /> 
         </Setter.Value> 
        </Setter> 
       </Style> 
      </Border.Style> 
     </Border> 
     <Border Background="{TemplateBinding Background}"> 
      <ContentPresenter 
       HorizontalAlignment="Center" 
       VerticalAlignment="Center" 
       Margin="5,2,5,0" 
       /> 
     </Border> 
    </Grid> 
    <ControlTemplate.Triggers> 
     <Trigger Property="IsPressed" Value="True"> 
      <Setter TargetName="BackgroundBorder" Property="Effect"> 
       <Setter.Value> 
        <DropShadowEffect 
         Color="Black" 
         Direction="320" 
         ShadowDepth="0" 
         BlurRadius="0" 
         Opacity="0" 
         /> 
       </Setter.Value> 
      </Setter> 
     </Trigger> 
    </ControlTemplate.Triggers>   
</ControlTemplate> 
+0

ありがとうございました! –

関連する問題