WPF后台代码实现TextBlock滚动条

 

方法一:

常规的WPF操作:

        <ScrollViewer Width="300" Height="150" BorderBrush="Brown" BorderThickness="2" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
            <TextBlock x:Name="_txtb" Background="HotPink" TextWrapping="Wrap" Foreground="Wheat"/>
        </ScrollViewer>

后台代码:

_txtb.Text = "1\r\n7\r\n6\r\n5dsaadsfjhduhtndsadjdhsajhscadsadsadas哈哈哈你\r\n4\r\n3\r\n2\r\n1\r\n0\r\n1\r\n2";

 

方法二:

后台直接写:       

        public MainWindow()
     { InitializeComponent();
var outer = new ScrollViewer(); outer.Background = Brushes.Gray; outer.Width = 300; outer.Height = 100; var inner = new TextBlock(); inner.Background = Brushes.Orange; inner.Foreground = Brushes.White; inner.Width = 200; outer.Content = inner; outer.MaxHeight = 80; inner.Text = "1\r\n7\r\n6\r\n5dsaadsfjhduhtndsadjdhsajhscadsadsadas哈哈哈你\r\n4\r\n3\r\n2\r\n1\r\n0\r\n1\r\n2"; // 此行用于文本内容根据设定宽度,当一行过长时自动换行 // inner.TextWrapping = TextWrapping.Wrap;
outer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto; outer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto; // 此句慎用呀,此处主要为了在界面上显示出结果 this.Content = outer; }

 

 

posted @ 2019-08-16 14:20  Carcar019  阅读(2835)  评论(0编辑  收藏  举报