2017-03-23 6 views
0

EDIT:私が視覚的に何をすべきかの明確な例として、Win 7電卓を開き、キーをクリックしてください。それはオレンジ色に咲くが、リリース時に元に戻って戻る。ストーリーボードボーダーをアニメーションしますが、同じターゲットの背景ではありません

ストーリーボードとカラーアニメーションを使用して、カスタムボーダーでオーバーレイされたWPFボタンをアニメートしようとしています。カスタムボタンテンプレートが機能します。トリガは機能します。別の財産をターゲットにしていると、私はストーリーボードを稼働させることさえできます。最初のコードブロックは、境界線のブラシアニメーションを持つスタイルで、プレス時にボタンの境界線を変更します。

<Style x:Key="CustomButton" TargetType="{x:Type Button}" BasedOn="{x:Null}"> 
     <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}" 
           Margin="{TemplateBinding Padding}" 
           VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
           RecognizesAccessKey="True"/> 
        </Grid> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsPressed" Value="True" > 
          <Trigger.EnterActions> 
           <BeginStoryboard> 
            <Storyboard> 
             <ColorAnimation Storyboard.TargetProperty="(BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="Border" To="Red" > 

             </ColorAnimation> 

            </Storyboard> 

           </BeginStoryboard> 

          </Trigger.EnterActions> 

         </Trigger>  

次に同じボタン要素、ボーダー、その背景プロパティをターゲットと同じ鋳型であるが、これは実行されますが、プレスに影響を与えずに、保持する又はさもなければ。どうして?

<Style x:Key="CustomButton" TargetType="{x:Type Button}" BasedOn="{x:Null}"> 
     <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}" 
           Margin="{TemplateBinding Padding}" 
           VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
           RecognizesAccessKey="True"/> 
        </Grid> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsPressed" Value="True" > 
          <Trigger.EnterActions> 
           <BeginStoryboard> 
            <Storyboard> 
             <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" Storyboard.TargetName="Border" To="Red" > 

             </ColorAnimation> 

            </Storyboard> 

           </BeginStoryboard> 

          </Trigger.EnterActions> 

         </Trigger> 



This isnt a dependency hierarchy issue as I can tell, since if i put a plain old setter in the trigger, targeting Border by name, and its Background property, the background dies indeed change on press. For this reason, i I don't think an event trigger is necessary because other changes trigger as I expect: 

押されたときに私が欲しいの動作は、カラーアニメーションであり、それが正常に行われますが、ない背景を持つ、唯一の外側のボーダー。そして、ターゲット名Borderの背景は、実際にはプレス時に変更されますが、Triggerブロックのプレーンなセッターに入れてしまえば、テンプレートの外で背景の変更がオーバーライドされているようなことはありません。ご意見ありがとうございます。

EDIT:テンプレート全体とスタイルを使用するボタンの例を追加することを決定しました。以下のコードは、問題の子プロパティを対象とするシンプルなセッターです。

<Style x:Key="CustomButton" TargetType="{x:Type Button}" BasedOn="{x:Null}"> 
     <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}" 
           Margin="{TemplateBinding Padding}" 
           VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
           RecognizesAccessKey="True"/> 
        </Grid> 
        <ControlTemplate.Triggers> 

         <Trigger Property="IsMouseOver" Value="true"> 
          <Setter Property="Background" Value="{StaticResource OnHoverBrush}" TargetName="Border"/> 
         </Trigger> 
         <Trigger Property="IsEnabled" Value="true"> 
          <Setter Property="BorderBrush" Value="#898989" TargetName="Border"/> 
         </Trigger> 


         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Background" Value="{StaticResource CustomDisableBrush}" TargetName="Border" /> 
          <Setter Property="BorderBrush" Value="#898989" TargetName="Border"/> 
          <Setter Property="Foreground" Value="Gray"/> 
         </Trigger> 

         <Trigger Property="IsPressed" Value="True" > 
          <Setter Property="Background" TargetName="Border" Value="Linen"/> 
         </Trigger> 

        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

と私は.NET 4.0以上を対象にコンパイルBUTTON

<Button x:Name="button0" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" Focusable="False" FontSize="40" 
        Click="OnNumberKeyClick" Style="{StaticResource CustomButton}"> 
       <Button.Background> 
        <LinearGradientBrush EndPoint="0,1" StartPoint="0,0"> 
         <GradientStop Color="#FFFAF3F3" Offset="0"/> 
         <GradientStop Color="#FFDDDDDD" Offset="0.5"/> 
         <GradientStop Color="#FF7A8197" Offset="1"/> 
        </LinearGradientBrush> 
       </Button.Background> 
       <!--BUTTON CONTENT--> 
       <Viewbox Stretch="Uniform"> 
        <TextBlock Text= "0"/> 
       </Viewbox> 
      </Button> 

答えて

1

。あなたのコードは正常に動作します。

しかし、.NET 3.5では、Border.Backgroundにデフォルト値がないようです(.NET 4.0以降のデフォルトは透過です)。したがって、その値を最初にSolidColorBrushに設定する必要があります。 MSDN Border.Background

プロパティ値 タイプ:System.Windows.Media.Brush 背景を描くブラシ。このプロパティにはデフォルト値はありません。私はそのに初期化問題はないと思うので、

+0

私はあなたの思考を好むが、私はクリスW下のコメントでは4.5.2 :( をターゲットにしています、私は正常にプロパティをターゲットにしています。切断がありますセッターがValueと呼ぶものと、カラーアニメーションのプロパティパスが探しているものとの間にあり、SoloidColorBrush.Color式で調整されていない場合 – schulmaster

+0

ああ、編集した投稿を見ました。ボタンをクリックします。アニメーションTargetPropertyに示されているようなColorプロパティはありません。トリガーは、LinearGradientBrushをSolidColorBrushリネンで完全に置き換えるために機能します。 – Hej

+0

はい!ありがとう!私が自分自身にもたらした助けようとしているすべての人に隠れている。あなたとChrisWの両方に感謝します。 – schulmaster

関連する問題