滾動以下筆者推薦的文本塊,因爲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,所以它可以在你的情況就發生了變化。這需要我做一點,所以如果你發現它真的有用,請給出答案。
通過「水平滾動」,你的意思是自動像一個自動收錄機或其他東西? – 2010-08-19 19:40:30
我的意思是使文本在文本框內像滾動字幕一樣滾動 – TimothyP 2010-08-20 08:11:07