当VisualState设置为" PointerOver"时更改文本,

时间:2015-02-10 14:32:08

标签: wpf wpf-controls

我正在ControlTemplate处理Button 这是我当前的代码,它改变了按钮的颜色。

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1.Styles">

    <ControlTemplate x:Key="ButtonBrushAnimation" TargetType="Button">
        <Grid >
            <TextBlock x:Name="textBlock" Width="80" Height="30" Text="AAA" />

            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">

                    <VisualStateGroup.Transitions>

                        <!--Take one half second to transition to the PointerOver state.-->
                        <VisualTransition To="PointerOver" GeneratedDuration="0:0:0.5"/>

                    </VisualStateGroup.Transitions>

                <VisualState x:Name="PointerOver">
                    <Storyboard>
                        <ColorAnimation Storyboard.TargetName="ButtonBrush" 
                        Storyboard.TargetProperty="Color" To="Red" />
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>

        <Grid.Background>
            <SolidColorBrush x:Name="ButtonBrush" Color="Green"/>
        </Grid.Background>
    </Grid>
</ControlTemplate>
</ResourceDictionary>

我想更改textBlock事件PointerOver控件的文字属性 我该怎么办? 谢谢!

1 个答案:

答案 0 :(得分:1)

StringAnimationUsingKeyFrames添加到Storyboard,如下所示:

<Storyboard>
  <ColorAnimation Storyboard.TargetName="ButtonBrush" Storyboard.TargetProperty="Color" To="Red" />
     <StringAnimationUsingKeyFrames Storyboard.TargetName="textBlock" Storyboard.TargetProperty="Text">
         <DiscreteStringKeyFrame Value="BBB"  KeyTime="0:0:0" />
     </StringAnimationUsingKeyFrames>
 </Storyboard>

您可以根据更改文本的具体时间设置KeyTime属性值。