I have a textbox wrapped by a scrollviewer. Is there a way to automatically scroll down to the bottom when the text of the textbox changes? I hope don't violate the mvvm rules.
<ScrollViewer x:Name="LogSV" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<TextBox x:Name="LogText" Text="{Binding Log}" IsReadOnly="True" Focusable="False"/>
</ScrollViewer>
You could subscribe to the PropertyChanged
event of the TextBox
and change the scroll if the property name is Text
.
private readonly ScrollViewer _scrollViewer;
public UserControl()
{
InitializeComponent();
this.FindControl<TextBox>("LogText").PropertyChanged += TextBoxPropertyChanged;
_scrollViewer = this.FindControl<ScrollViewer>("LogSV");
}
private void TextBoxPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e)
{
if(e.Property.Name == "Text")
{
_scrollViewer.ScrollToEnd();
}
}