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.
fonte
2015-04-03 07:02:48
Per "scorrere orizzontalmente", intendi automaticamente come un nastro o qualcos'altro? –
Intendo far scorrere il testo all'interno del TextBox come un riquadro di selezione – TimothyP