2009-07-26 41 views
3

只是想知道是否有人知道如何如果您希望交换的整体风格动画从一个风格到另一个即从NormalStyle去ActiveStyle当用户集中在文本框中动画从一个风格到另一个

<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> 

回答

1

,我想你可能必须做的代码:

<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; 
    } 

否则,你几乎仅限于触发来回..

<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 
相关问题