I'm wondering if anybody knows any good (free) behaviors for Blend/Silverlight 4
Specifically I'm looking for a behavior that I can drop on a TextBlock to make it scroll horizontally or a behavior that will "flash" text in a TextBlock (blinking text). But I'd love to hear about any behaviors you've been using or know about.
As an example, I've got a 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 upon, but I'm really interested in what other people have come up with.
For scrolling the textblock I recommend the following, because translatetransform and clipping is not so smooth, let's add 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>
And now the converter:
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();
}
}
And the best of this way is as you see, you can manage what to do at the end of scrolling.
And now adding the blinking behaviour
<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>
where the behaviour is easier:
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();
}
}
Note: I did it for Windows 10, so it could change a bit in your case. It take me a bit to make it, so mark as answer if you find it really useful.