2010-06-02 25 views
5

Mi chiedo se qualcuno sa qualsiasi comportamento buono (gratuito) per Blend/Silverlight 4Comportamenti per Blend (Silverlight 4)

In particolare sto cercando un comportamento che posso cadere su un TextBlock fare scorre in orizzontale o un comportamento che "lampeggerà" il testo in un TextBlock (testo lampeggiante). Ma mi piacerebbe sapere di tutti i comportamenti che hai usato o conosci.

Per fare un esempio, ho una base "testo lampeggiante" il comportamento

public class FlashTextBehavior : Behavior<TextBlock> 
{ 
    Timer flashTimer; 

    public FlashTextBehavior() 
    { 

    } 

    protected override void OnAttached() 
    { 
     base.OnAttached(); 
     flashTimer = new Timer(new TimerCallback((o) => 
     { 
      Dispatcher.BeginInvoke(() => 
      { 
       if (AssociatedObject.Visibility == Visibility.Visible) 
        AssociatedObject.Visibility = Visibility.Collapsed; 
       else 
        AssociatedObject.Visibility = Visibility.Visible; 
      });    
     }), null, 0, 750); 
    } 

    protected override void OnDetaching() 
    { 
     if (flashTimer != null) 
      flashTimer.Dispose(); 

     base.OnDetaching(); 
    } 
} 

Naturalmente può essere migliorato, ma sono davvero interessato a ciò che gli altri persone hanno escogitato .

+0

Per "scorrere orizzontalmente", intendi automaticamente come un nastro o qualcos'altro? –

+0

Intendo far scorrere il testo all'interno del TextBox come un riquadro di selezione – TimothyP

risposta

1

Per scorrere il blocco di testo vi consiglio il seguente, perché TranslateTransform e ritaglio non è così liscia, aggiungiamo in XAML:

<ScrollViewer Margin="40" Width="100" VerticalAlignment="Top" HorizontalAlignment="Left" VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Hidden" HorizontalScrollMode="Enabled"> 
     <i:Interaction.Behaviors> 
      <behaviors:ScrollHorizontalBehavior/> 
     </i:Interaction.Behaviors> 
     <TextBlock Foreground="White" Text="asdfasdfasdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf HALF asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf LAST" > 
     </TextBlock> 
    </ScrollViewer> 

E ora il convertitore:

public class ScrollHorizontalBehavior : DependencyObject, IBehavior 
{ 
    public DependencyObject AssociatedObject { get; private set; } 

    public void Attach(DependencyObject associatedObject) 
    { 
     AssociatedObject = associatedObject; 
     InitializeTranslation(); 
    } 

    private DispatcherTimer UITimer { get; set; } 
    private void InitializeTranslation() 
    { 
     var element = AssociatedObject as ScrollViewer; 
     UITimer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(30) }; 
     UITimer.Tick += (s, e) => 
     { 
      var newvalue = element.HorizontalOffset + 20; 
      if (newvalue > element.ScrollableWidth) 
       newvalue = 0; 
      element.ChangeView(newvalue, null, null); 
     }; 
     UITimer.Start(); 
    } 

    public void Detach() 
    { 
     if (UITimer != null) UITimer.Stop(); 
    } 
} 

E la cosa migliore in questo modo, come vedi, puoi gestire cosa fare alla fine dello scorrimento.

E ora aggiungendo il comportamento lampeggiante

<TextBlock Foreground="White" Text="asdfasdfasdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf HALF asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf LAST" > 
      <i:Interaction.Behaviors> 
      <behaviors:BlinkingBehavior/> 
     </i:Interaction.Behaviors> 
    </TextBlock> 

in cui il comportamento è più facile:

public class BlinkingBehavior : DependencyObject, IBehavior 
{ 
    public DependencyObject AssociatedObject { get; private set; } 

    public void Attach(DependencyObject associatedObject) 
    { 
     AssociatedObject = associatedObject; 
     InitializeBlinking(); 
    } 

    bool firstcolor = true; 
    private void InitializeBlinking() 
    { 
     var element = AssociatedObject as TextBlock; 

     var brushA = new SolidColorBrush(Colors.Red); 
     var brushB = new SolidColorBrush(Colors.White); 

     UITimer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(1000) }; 
     UITimer.Tick += (s, e) => 
     { 
      element.Foreground = firstcolor ? brushA : brushB; 
      firstcolor = !firstcolor; 
     }; 
     UITimer.Start(); 
    } 

    private DispatcherTimer UITimer { get; set; } 

    public void Detach() 
    { 
     if (UITimer != null) UITimer.Stop(); 
    } 
} 

Nota: L'ho fatto per Windows 10, quindi potrebbe cambiare un po 'nel tuo caso. Mi ci vuole un po 'per farlo, quindi segna come risposta se lo trovi davvero utile.

Problemi correlati