Silverlight 4 - Come posso cambiare il colore di sfondo pulsante quando è focalizzato con uno stile tasto implicito?
-
27-09-2019 - |
Domanda
Sto avendo una grande quantità di difficoltà cercando di raggiungere qualcosa che dovrebbe essere banale. Sto usando un implicito stile del pulsante definito in un file di risorse globale XAML. Voglio solo cambiare il colore del pulsante focalizzata sfondo per rosso con un ColorAnimation. Ho provato un certo numero di combinazioni diverse a Storyboard.TargetProperty
e Storyboard.TargetName
e niente ha funzionato. Come posso raggiungere questo obiettivo?
Grazie in anticipo.
<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>
...
Soluzione
Dato che non ho il resto del tuo stile Ho fatto questo con due bordi e un ContentPresenter. Questa anima la sfondo del pulsante dal verde al rosso, una volta messo a fuoco.
<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>
Altri suggerimenti
Alcune buone risposte qui:
Stile del MouseOver su un pulsante Silverlight / WPF http://forums.silverlight.net/forums/p/186402/427878. aspx