2009-07-26 4 views
3

me demandais si quelqu'un sait comment animer d'un style à un autre-à-dire allant de NormalStyle à ActiveStyle lorsque l'utilisateur se concentre sur la zone de texteAnimer d'un style à un autre

<Style x:key="NormalStyle" TargetType="{x:Type TextBox}"> 
    <Setter Property="BorderBrush" Value="Gray" /> 
    <Setter Property="BorderThickness" Value="2" /> 
</Style> 

<Style x:key="ActiveStyle" TargetType="{x:Type TextBox}" BasedOn="{StaticResource NormalStyle}"> 
    <Setter Property="BorderBrush" Value="Green" /> 
    <Setter Property="BorderThickness" Value="4" /> 
</Style> 

Répondre

1

Si vous voulez échanger tout le style , je pense que vous pourriez avoir à le faire dans le code:

<StackPanel> 
    <TextBox Style="{StaticResource NormalStyle}" GotFocus="TextBox_GotFocus" LostFocus="TextBox_LostFocus"/> 
    <TextBox Style="{StaticResource NormalStyle}" GotFocus="TextBox_GotFocus" LostFocus="TextBox_LostFocus"/> 
</StackPanel> 
private Style focusedStyle; 
    private Style normalStyle; 

    public MainWindow() 
    { 
     InitializeComponent(); 

     focusedStyle = FindResource("ActiveStyle") as Style; 
     normalStyle = FindResource("NormalStyle") as Style; 
    } 

    private void TextBox_GotFocus(object sender, RoutedEventArgs e) 
    { 
     ((TextBox)sender).Style = focusedStyle; 
    } 

    private void TextBox_LostFocus(object sender, RoutedEventArgs e) 
    { 
     ((TextBox)sender).Style = normalStyle; 
    } 

Sinon, vous êtes à peu près limité à déclenchement avant et en arrière ..

<TextBox Text="1" > 
     <TextBox.Style> 
      <Style BasedOn="{StaticResource NormalStyle}" TargetType="{x:Type TextBox}"> 
       <Style.Triggers> 
        <EventTrigger RoutedEvent="GotFocus"> 
         <BeginStoryboard> 
          <Storyboard> 
           <ColorAnimation Storyboard.TargetProperty="BorderBrush.Color" To="Green" Duration="0:0:0.1" /> 
          </Storyboard> 
         </BeginStoryboard> 
        </EventTrigger> 
        <EventTrigger RoutedEvent="LostFocus"> 
         <BeginStoryboard> 
          <Storyboard> 
           <ColorAnimation Storyboard.TargetProperty="BorderBrush.Color" To="Gray" Duration="0:0:0.1" /> 
          </Storyboard> 
         </BeginStoryboard> 
        </EventTrigger> 
       </Style.Triggers> 
      </Style> 
     </TextBox.Style 
Questions connexes