2010-06-02 11 views
5

Zastanawiam się, czy ktoś zna jakieś dobre (bezpłatny) dla zachowania Mieszanka/Silverlight 4Zachowania dla Blend (Silverlight 4)

szczególności szukam na zachowania, które może wpadnę na TextBlock aby przewija się w poziomie lub zachowuje się, co spowoduje "flashowanie" tekstu w TextBlock (migający tekst). Ale chciałbym usłyszeć o wszelkich zachowaniach, których używasz lub o których wiesz.

Jako przykład mam bardzo podstawowe „migający tekst” zachowanie

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

Oczywiście można je ulepszyć, ale jestem naprawdę zainteresowany tym, co inni ludzie wymyślić .

+0

Przez "przewijanie w poziomie" rozumiesz automatycznie, jak taśmę-tasiemkę czy coś innego? –

+0

Mam na myśli, aby przewijanie tekstu w obrębie TextBox było jak marquee – TimothyP

Odpowiedz

1

do przewijania textblock zalecam następujący, ponieważ translatetransform i obcinanie nie jest tak gładka, dodajmy w 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> 

A teraz konwerter:

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

A najlepiej w ten sposób możesz zobaczyć, co możesz zrobić na końcu przewijania.

A teraz dodanie migający zachowanie

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

gdzie zachowanie jest łatwiej:

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

UWAGA: Zrobiłem to dla systemu Windows 10, więc może to zmienić trochę w Twoim przypadku. Trochę mnie to zajmuje, więc oznacz jako odpowiedź, jeśli uznasz to za naprawdę użyteczne.

Powiązane problemy