Silverlight 4 - ¿Cómo puedo cambiar el color del botón del fondo cuando se enfoca con un estilo de botón implícito?

StackOverflow https://stackoverflow.com/questions/4064363

Pregunta

Estoy teniendo una gran cantidad de dificultades para tratar de lograr algo que debería ser trivial. Estoy usando un estilo de botón implícito definido en un archivo de recursos global XAML. Sólo quiero cambiar el color de fondo del botón centró a rojo con un ColorAnimation. He intentado un número de diferentes combinaciones en Storyboard.TargetProperty y Storyboard.TargetName y nada ha funcionado. ¿Cómo puedo lograr esto?

Gracias de antemano.

<Style TargetType="Button" >
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="Button">
            <Grid x:Name="grid" RenderTransformOrigin="0.5,0.5">
                <Grid.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform/>
                        <SkewTransform/>
                        <RotateTransform/>
                        <TranslateTransform/>
                    </TransformGroup>
                </Grid.RenderTransform>
                <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="FocusStates">
            <VisualState x:Name="Focused" >
                <Storyboard>
                    <ColorAnimation Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="Button" From="Green" To="Red" Duration="00:00:01" />
                </Storyboard>
            </VisualState>
            <VisualState x:Name="Unfocused"/>
            </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
        ...
¿Fue útil?

Solución

Dado que no tengo el resto de su estilo I hecho esto con dos bordes y un ContentPresenter. Esto anima el fondo del botón de verde a rojo una vez centrado.

<Style TargetType="Button" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid x:Name="grid" RenderTransformOrigin="0.5,0.5">
                    <Grid.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform/>
                            <SkewTransform/>
                            <RotateTransform/>
                            <TranslateTransform/>
                        </TransformGroup>
                    </Grid.RenderTransform>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="FocusStates">
                            <VisualState x:Name="Focused">
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="border"
                                                    Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                                                    From="Green"
                                                    To="Red"
                                                    Duration="0:0:1" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Unfocused"/>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Border BorderBrush="Transparent" BorderThickness="1" CornerRadius="4">
                        <Border x:Name="border" Background="White" BorderBrush="Black" BorderThickness="1" CornerRadius="4">
                        </Border>
                    </Border>
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top