Behavior for Blend (Silverlight 4)

I am wondering if anyone knows the good (free) behaviors for Blend / Silverlight 4

In particular, I am looking for a behavior that I can reset to a TextBlock so that it scrolls horizontally or a behavior that will “blink” in a text block (blinking text). But I would like to hear about any behavior that you used or know.

As an example, I have very basic flashing text behavior

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

Of course, it can be improved, but I'm really interested in what people came up with.

+5
source share
1 answer

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

:

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

, , , .

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

:

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

. Windows 10, . , , , .

+1

All Articles