2010-06-02 11 views
5

Me pregunto si alguien sabe alguna buenos comportamientos (gratuita) para Blend/Silverlight 4Comportamientos de mezcla (Silverlight 4)

Específicamente estoy en busca de un comportamiento que pueda caer en un TextBlock hacer se desplaza horizontalmente o un comportamiento que "destellará" texto en un TextBlock (texto parpadeante). Pero me encantaría saber acerca de cualquier comportamiento que haya estado usando o que sepa.

A modo de ejemplo, tengo una muy básico "intermitente texto" comportamiento

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

Por supuesto que se puede mejorar, pero estoy realmente interesado en lo que otras personas han llegar a .

+0

Por "desplazamiento horizontal", ¿te refieres automáticamente a una cinta de teletipo u otra cosa? –

+0

Me refiero a hacer que el texto se desplace dentro del TextBox como una marquesina – TimothyP

Respuesta

1

Para desplazar el bloque de texto que recomiendo lo siguiente, porque TranslateTransform y el recorte no es tan suave, vamos a añadir en 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> 

Y ahora el convertidor:

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

Y lo mejor de De esta forma, como verá, puede administrar qué hacer al final del desplazamiento.

Y ahora añadiendo el comportamiento intermitente

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

donde el comportamiento es más fácil:

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: Lo hice para Windows 10, por lo que podría cambiar un poco, en su caso. Me llevó un poco hacerlo, así que marque como respuesta si lo encuentra realmente útil.

Cuestiones relacionadas