方法一: 常規的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; }