给TextBlock加上滚动条

今天想让Textbook在内容越界的时候显示滚动条,找了半天属性没有找到。原来与Border一样,滚动条也是作为一个单独的附件加上的,呵呵,有趣。显示上下左右滚动条的方法:

ScrollViewer.HorizontalScrollBarVisibility="Auto" 

ScrollViewer.VerticalScrollBarVisibility="Auto"

Source Code


<ScrollViewer Height="300" MinWidth="360">
        <ScrollViewer.Content>
            <TextBlock Name="txtMessages" Text="Messages"  />
        </ScrollViewer.Content>
    </ScrollViewer>


可以使用以下代码实现 WPF TextBlock 的自动滚动动画: ```xaml <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="450" Width="800"> <Grid> <ScrollViewer VerticalScrollBarVisibility="Auto"> <StackPanel x:Name="stackPanel"> <TextBlock x:Name="textBlock" TextWrapping="Wrap" FontSize="20" Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod, justo vel commodo lacinia, velit magna bibendum sapien, ut commodo elit nulla vitae turpis."></TextBlock> </StackPanel> </ScrollViewer> </Grid> </Window> ``` ```csharp using System.Windows; using System.Windows.Controls; using System.Windows.Media.Animation; namespace WpfApp1 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); // 设置 TextBlock 的高度为 ScrollViewer 的高度 textBlock.Height = stackPanel.ActualHeight; // 创建 DoubleAnimation 对象,设置动画属性 DoubleAnimation doubleAnimation = new DoubleAnimation(); doubleAnimation.From = 0; doubleAnimation.To = textBlock.ActualHeight - stackPanel.ActualHeight; doubleAnimation.Duration = new Duration(new System.TimeSpan(0, 0, 10)); doubleAnimation.RepeatBehavior = RepeatBehavior.Forever; // 创建 ScrollViewer 的垂直滚动条的动画 ScrollViewer scrollViewer = (ScrollViewer)stackPanel.Parent; scrollViewer.BeginAnimation(ScrollViewer.VerticalOffsetProperty, doubleAnimation); } } } ``` 这段代码会在 TextBlock 中显示一段文本,并且使用 DoubleAnimation 对象创建一个动画,使得 ScrollViewer 的垂直滚动条自动滚动,从而实现 TextBlock 的自动滚动动画。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值