可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a TextBlock
in WPF. I write many lines to it, far exceeding its vertical height. I expected a vertical scroll bar to appear automatically when that happens, but it didn't. I tried to look for a scroll bar property in the Properties pane, but could not find one.
How can I make vertical scroll bar created automatically for my TextBlock
once its contents exceed its height?
Clarification: I would rather do it from the designer and not by directly writing to the XAML.
回答1:
Wrap it in a scroll viewer:
NOTE this answer applies to a TextBlock
(a read-only text element) as asked for in the original question.
If you want to show scroll bars in a TextBox
(an editable text element) then use the ScrollViewer
attached properties:
Valid values for these two properties are Disabled
, Auto
, Hidden
and Visible
.
回答2:
can use the following now:
SOME TEXT
回答3:
Something better would be:
This makes sure that the text in your textblock does not overflow and overlap the elements below the textblock as may be the case if you do not use the grid. That happened to me when I tried other solutions even though the textblock was already in a grid with other elements. Keep in mind that the width of the textblock should be Auto and you should specify the desired with in the Grid element. I did this in my code and it works beautifully. HTH.
回答4:
This is way to use the scrolling TextBox in XAML and use it as a text area.
回答5:
This answer describes a solution using MVVM.
This solution is great if you want to add a logging box to a window, that automatically scrolls to the bottom each time a new logging message is added.
Once these attached properties are added, they can be reused anywhere, so it makes for very modular and reusable software.
Add this XAML:
Add this attached property:
public static class TextBoxApppendBehaviors { #region AppendText Attached Property public static readonly DependencyProperty AppendTextProperty = DependencyProperty.RegisterAttached( "AppendText", typeof (string), typeof (TextBoxApppendBehaviors), new UIPropertyMetadata(null, OnAppendTextChanged)); public static string GetAppendText(TextBox textBox) { return (string)textBox.GetValue(AppendTextProperty); } public static void SetAppendText( TextBox textBox, string value) { textBox.SetValue(AppendTextProperty, value); } private static void OnAppendTextChanged( DependencyObject d, DependencyPropertyChangedEventArgs args) { if (args.NewValue == null) { return; } string toAppend = args.NewValue.ToString(); if (toAppend == "") { return; } TextBox textBox = d as TextBox; textBox?.AppendText(toAppend); textBox?.ScrollToEnd(); } #endregion }
And this attached property (to clear the box):
public static class TextBoxClearBehavior { public static readonly DependencyProperty TextBoxClearProperty = DependencyProperty.RegisterAttached( "TextBoxClear", typeof(bool), typeof(TextBoxClearBehavior), new UIPropertyMetadata(false, OnTextBoxClearPropertyChanged)); public static bool GetTextBoxClear(DependencyObject obj) { return (bool)obj.GetValue(TextBoxClearProperty); } public static void SetTextBoxClear(DependencyObject obj, bool value) { obj.SetValue(TextBoxClearProperty, value); } private static void OnTextBoxClearPropert