2016-05-02 15 views
1

私はWPFが初めてです。ボタンのテキストと色をMouseEnterに変更したい。ここ は、色を変更するために私のコードです:WPFを使用してマウスのボタンのテキストと色を変更する方法

<Style x:Key="btnClose" TargetType="{x:Type Button}" > 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button" x:Name="btnClose"> 
       <Border Name="btnCloseBorder" CornerRadius="7" Background="Red" BorderBrush="#ccc" BorderThickness="0,1,1,0" > 
        <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsMouseOver" Value="True"> 
         <Setter Property="Button.Content"> 
          <Setter.Value> 
           <ContentElement ???/> 
          </Setter.Value> 
         </Setter> 
        </Trigger> 
        <EventTrigger RoutedEvent="Mouse.MouseEnter"> 
         <BeginStoryboard> 
          <Storyboard> 
           <ColorAnimation From="Red" To="#FF7F7F" Duration="0:0:0.5" Storyboard.TargetName="btnCloseBorder" Storyboard.TargetProperty="Background.Color"/> 
          </Storyboard> 
         </BeginStoryboard> 
        </EventTrigger> 
        <EventTrigger RoutedEvent="Mouse.MouseLeave"> 
         <BeginStoryboard> 
          <Storyboard> 
           <ColorAnimation From="#FF7F7F" To="Red" Duration="0:0:0.5" Storyboard.TargetName="btnCloseBorder" Storyboard.TargetProperty="Background.Color"/> 
          </Storyboard> 
         </BeginStoryboard> 
        </EventTrigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<Button Style="{StaticResource btnClose}" Name="btnClose" Content="" HorizontalAlignment="Left" Margin="274,0,0,0" VerticalAlignment="Top" Width="18" Height="21"/> 

それは魔法のように動作します。しかし、私はテキストを変更することができませんでした。

ゲストColorAnimationタグの後にする必要があります。しかし、私はどのタグを使うべきかを知らない。

ボタンテキストをMouseEnterMouseLeaveに変更するにはどうすればよいですか?

+0

'Button.Content'を設定してください –

+0

あなたはKeyFrameAnimationが必要です –

答えて

0

ContentControlを明示的に作成する必要はありません。任意のオブジェクトにButton.Contentを設定できます。オブジェクトがUIElementでない場合、暗黙のTextBlockが作成され、テキストはValue.ToString()に設定されます。 UIElementの場合は、レンダリングにはOnRenderメソッドが使用されます。

あなたのケースでは、単に設定値を文字列に設定することができます。 IsMouseOverがfalseになったときに、このトリガーは自動的に以前の値に戻すということ

<Trigger Property="IsMouseOver" Value="True"> 
    <Setter Property="Button.Content" Value="Your Text"> 
    </Setter> 
</Trigger> 

は注意してください。

+0

' 'と' 'の間にコードを追加しました。しかし、それは動作しません。本当の場所ですか? –

+0

なぜイベントトリガーが必要ですか?簡単なトリガーはうまく動作します。 –

+0

アニメーションで色とテキストを変更したいのであなたはもっと良いアプローチを知っていますか? –

0

私の答えが見つかりました。私は単純にButtonタグにContentを削除し、以下のようなStyleを変更:

<Style x:Key="btnClose" TargetType="{x:Type Button}" > 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button" x:Name="btnClose"> 
       <Border Name="btnCloseBorder" CornerRadius="7" Background="Red" BorderBrush="#ccc" BorderThickness="0,1,1,0" > 
        <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsMouseOver" Value="True"> 
         <Setter Property="Button.Content" Value="Your Text"> 
         </Setter> 
        </Trigger> 
        <EventTrigger RoutedEvent="Mouse.MouseEnter"> 
         <BeginStoryboard> 
          <Storyboard > 
           <ColorAnimation From="Red" To="#FF7F7F" Duration="0:0:0" Storyboard.TargetName="btnCloseBorder" Storyboard.TargetProperty="Background.Color"/> 
          </Storyboard> 
         </BeginStoryboard> 
        </EventTrigger> 
        <EventTrigger RoutedEvent="Mouse.MouseLeave"> 
         <BeginStoryboard> 
          <Storyboard> 
           <ColorAnimation From="#FF7F7F" To="Red" Duration="0:0:0.5" Storyboard.TargetName="btnCloseBorder" Storyboard.TargetProperty="Background.Color"/> 
          </Storyboard> 
         </BeginStoryboard> 
        </EventTrigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 

    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="true"> 
      <Setter Property = "Foreground" Value="White"/> 
      <Setter Property = "Button.Content" Value="Some new text..."/> 
     </Trigger> 
    </Style.Triggers> 

</Style> 

<Button Style="{StaticResource btnClose}" Name="btnClose" HorizontalAlignment="Left" Margin="274,0,0,0" VerticalAlignment="Top" Width="18" Height="21"/> 

私はそれが役に立つことを願っています。