Note that there are some explanatory texts on larger screens.

plurals
  1. POSingle-line wpf textbox horizontal scroll to end
    primarykey
    data
    text
    <p>I have a templated listbox which template among other things contains a wpf textbox too. The data is provided to the listbox through ItemsSource.</p> <p>The textboxes display filepaths and these are usally quite long. I want when the textboxes are loaded to show the end of the filepaths.</p> <p>I tried a combination of <code>DataContextChanged</code> event and setting <code>HorizontalScrollBarVisibility</code> (using double.max or getting the real char length) but to no success. The DataContextChanged seems to be the correct event to use as it fires on each setting of the ItemsSource.</p> <p>Edit:</p> <p>Here is sample code to show when the suggestion by Lester works and when it doesnt. I am trying to have it work when the text is set through binding.</p> <pre><code>&lt;Window x:Class="WpfAppTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" Loaded="LoadedHandler"&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto"&gt;&lt;/RowDefinition&gt; &lt;RowDefinition Height="Auto"&gt;&lt;/RowDefinition&gt; &lt;/Grid.RowDefinitions&gt; &lt;TextBox Name="tbb" Width="50" Height="20" Text="{Binding Path=Str}" IsReadOnly="True" Grid.Column="0" Grid.Row="0" DataContextChanged="ContextChangedHandler"/&gt; &lt;ListBox SelectionMode="Single" x:Name="listBox" Grid.Column="0" Grid.Row="1" VerticalAlignment="Top"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;TextBox Width="50" Height="20" Text="{Binding Path=Str}" IsReadOnly="True" DataContextChanged="ContextChangedHandler"/&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;/Grid&gt; </code></pre> <p></p> <pre><code>public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); var obj = new SomeClass { Str = "qwetyuiuropqo[psdal;dkas;ldamzxn m,cnz128391" }; listBox.ItemsSource = new List&lt;SomeClass&gt; { obj }; tbb.DataContext = obj; } public class SomeClass { public string Str { get; set; } } private void LoadedHandler(object sender, RoutedEventArgs e) { var obj = new SomeClass { Str = "qwetyuiuropqo[psdal;dkas;ldamzxn m,cnz128391" }; listBox.ItemsSource = new List&lt;SomeClass&gt; { obj }; tbb.DataContext = obj; } private void ContextChangedHandler(object sender, DependencyPropertyChangedEventArgs e) { var textBox = sender as TextBox; if (textBox == null) return; textBox.CaretIndex = textBox.Text.Length; var rect = textBox.GetRectFromCharacterIndex(textBox.CaretIndex); textBox.ScrollToHorizontalOffset(rect.Right); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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