WPF后台代码实现TextBlock滚动条
程序员文章站
2022-05-29 10:29:42
方法一: 常规的WPF操作: 后台代码: 方法二: 后台直接写: ......
方法一:
常规的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; }