Non sono un grande sostenitore delle animazioni di WPF (a parte le demo ovviamente, il pulsante che ruota è ormai un classico...) però in alcuni casi le animazioni possono dare un tocco di "eleganza" anche alla più statica delle applicazioni.
Il frammento di codice che segue anima la proprietà StrokeDashOffset di un rettangolo racchiuso in un template associato attraverso FocusVisualStyle ottenendo l'effetto della classica linea animata che circonda il controllo col focus.

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="FocusVisualStyle.Window1"
    x:Name="Window"
    Title="Window1"
    Width="640" Height="480">

    <Window.Resources>
        <ControlTemplate x:Key="FocusTemplate" >
            <Rectangle Margin="-3" Stroke="Orange" StrokeThickness="2" 
                          RadiusX="2" RadiusY="2" StrokeDashArray="2,1" >
                <Rectangle.Triggers>
                    <EventTrigger RoutedEvent="Rectangle.Loaded" >
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation
                               To="3"Duration="0:0:1"RepeatBehavior="Forever" 
                                         Storyboard.TargetProperty="StrokeDashOffset" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Rectangle.Triggers>
            </Rectangle>
        </ControlTemplate>
        <Style x:Key="FocusStyle" TargetType="{x:Type Control}">
            <Setter Property="Template" Value="{StaticResource FocusTemplate}"/>
        </Style>
        <Style TargetType="TextBox">
            <Setter Property="FocusVisualStyle" Value="{StaticResource FocusStyle}"/>
        </Style>
    </Window.Resources>

    <Window.Background>
        <LinearGradientBrush EndPoint="0.507,1.167" StartPoint="0.493,-0.167">
            <GradientStop Color="#FF000000" Offset="0"/>
            <GradientStop Color="#FF6A70D5" Offset="1"/>
        </LinearGradientBrush>
    </Window.Background>

    <Grid x:Name="LayoutRoot">
        <TextBox Margin="206,67,234,0" VerticalAlignment="Top" Height="28" Text="TextBox" TextWrapping="Wrap" />
        <TextBox Height="28" Margin="206,110,234,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="TextBox"/>
    </Grid>
</Window>

Risultato:

image 

No code has been hurted for this demo... smile_wink

Technorati Tags: ,