Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Hmm this seemed like an interesting thing to implement so I took a crack at it. From some goggling it doesn't seem like there is a straight forward way to "tell" the Textbox to scroll itself to the end. So I thought of it a different way. All framework controls in WPF have a default Style/ControlTemplate, and judging by the looks of the Textbox control there must be a ScrollViewer inside which handles the scrolling. So, why not just work with a local copy of the default Textbox ControlTemplate and programmaticlly get the ScrollViewer. I can then tell the ScrollViewer to scroll its Contents to the end. Turns out this idea works.</p> <p>Here is the test program I wrote, could use some refactoring but you can get the idea by looking at it:</p> <p>Here is the XAML:</p> <pre><code>&lt;Window x:Class="WpfApplication3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication3="clr-namespace:WpfApplication3" Title="MainWindow" Height="350" Width="525"&gt; &lt;Window.Resources&gt; &lt;!--The default Style for the Framework Textbox--&gt; &lt;SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" /&gt; &lt;SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" /&gt; &lt;SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF" /&gt; &lt;SolidColorBrush x:Key="SolidBorderBrush" Color="#888" /&gt; &lt;ControlTemplate x:Key="MyTextBoxTemplate" TargetType="{x:Type TextBoxBase}"&gt; &lt;Border x:Name="Border" CornerRadius="2" Padding="2" Background="{StaticResource WindowBackgroundBrush}" BorderBrush="{StaticResource SolidBorderBrush}" BorderThickness="1"&gt; &lt;ScrollViewer Margin="0" x:Name="PART_ContentHost" /&gt; &lt;/Border&gt; &lt;ControlTemplate.Triggers&gt; &lt;Trigger Property="IsEnabled" Value="False"&gt; &lt;Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" /&gt; &lt;Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBackgroundBrush}" /&gt; &lt;Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}" /&gt; &lt;/Trigger&gt; &lt;/ControlTemplate.Triggers&gt; &lt;/ControlTemplate&gt; &lt;Style x:Key="MyTextBox" TargetType="{x:Type TextBoxBase}"&gt; &lt;Setter Property="SnapsToDevicePixels" Value="True" /&gt; &lt;Setter Property="OverridesDefaultStyle" Value="True" /&gt; &lt;Setter Property="KeyboardNavigation.TabNavigation" Value="None" /&gt; &lt;Setter Property="FocusVisualStyle" Value="{x:Null}" /&gt; &lt;Setter Property="MinWidth" Value="120" /&gt; &lt;Setter Property="MinHeight" Value="20" /&gt; &lt;Setter Property="AllowDrop" Value="true" /&gt; &lt;Setter Property="Template" Value="{StaticResource MyTextBoxTemplate}"&gt;&lt;/Setter&gt; &lt;/Style&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;WpfApplication3:AutoScrollTextBox x:Name="textbox" TextWrapping="Wrap" Style="{StaticResource MyTextBox}" VerticalScrollBarVisibility="Visible" AcceptsReturn="True" Width="100" Height="100"&gt;test&lt;/WpfApplication3:AutoScrollTextBox&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>And the code behind:</p> <pre><code>using System; using System.Windows; using System.Windows.Controls; namespace WpfApplication3 { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); for (int i = 0; i &lt; 10; i++) { textbox.AppendText("Line " + i + Environment.NewLine); } } } public class AutoScrollTextBox : TextBox { protected override void OnTextChanged(TextChangedEventArgs e) { base.OnTextChanged(e); // Make sure the Template is in the Visual Tree: // http://stackoverflow.com/questions/2285491/wpf-findname-returns-null-when-it-should-not ApplyTemplate(); var template = (ControlTemplate) FindResource("MyTextBoxTemplate"); var scrollViewer = template.FindName("PART_ContentHost", this) as ScrollViewer; //SelectionStart = Text.Length; scrollViewer.ScrollToEnd(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload