Skip to content

Instantly share code, notes, and snippets.

@DanielRose
Created June 4, 2014 13:48
Show Gist options
  • Save DanielRose/c94f11f1a972a453902e to your computer and use it in GitHub Desktop.
Save DanielRose/c94f11f1a972a453902e to your computer and use it in GitHub Desktop.
Scroll bug in AvalonEdit
<Window
x:Class="AvalonEditScrollBug.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:avalonEdit="clr-namespace:ICSharpCode.AvalonEdit;assembly=ICSharpCode.AvalonEdit"
Title="MainWindow"
Width="525"
Height="350">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Menu>
<MenuItem Click="OnClearText" Header="Clear text" />
<MenuItem Click="OnAddSingleLineText" Header="Add text (single line)" />
<MenuItem Click="OnAddMultiLineText" Header="Add text (many lines)" />
</Menu>
<avalonEdit:TextEditor
Name="PART_TextEditor"
Grid.Row="1"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto" />
</Grid>
</Window>
namespace AvalonEditScrollBug
{
using System;
using System.Windows;
using ICSharpCode.AvalonEdit.Document;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
public MainWindow()
{
this.InitializeComponent();
this.PART_TextEditor.Document.Changing += this.OnDocumentChanging;
this.PART_TextEditor.Document.Changed += this.OnDocumentChanged;
}
private void OnDocumentChanging(object sender, DocumentChangeEventArgs e)
{
this.ShouldAutoScroll = this.IsScrolledToEnd && e.Offset == this.PART_TextEditor.Document.TextLength;
}
private bool IsScrolledToEnd
{
get
{
return this.PART_TextEditor.VerticalOffset + this.PART_TextEditor.ViewportHeight >= this.PART_TextEditor.ExtentHeight;
}
}
private bool ShouldAutoScroll { get; set; }
private void OnDocumentChanged(object sender, DocumentChangeEventArgs e)
{
if (this.ShouldAutoScroll)
{
this.PART_TextEditor.ScrollToLine(this.PART_TextEditor.LineCount);
//ApplicationHelper.DoEvents();
//if (this.IsScrolledToEnd)
//{
// return;
//}
//TextEditor.TextArea.TextView.Redraw();
//ApplicationHelper.DoEvents();
//if (this.IsScrolledToEnd)
//{
// return;
//}
//var vl =
// TextEditor.TextArea.TextView.GetOrConstructVisualLine(
// this.Document.GetLineByNumber(this.Document.LineCount));
//TextEditor.TextArea.TextView.Redraw(vl);
//ApplicationHelper.DoEvents();
//if (this.IsScrolledToEnd)
//{
// return;
//}
//this.TextEditor.ScrollToLine(this.TextEditor.LineCount);
}
}
private void OnAddSingleLineText(object sender, RoutedEventArgs e)
{
this.PART_TextEditor.Document.Insert(
this.PART_TextEditor.Document.TextLength,
string.Format("{0}--- Line #{1} ---", Environment.NewLine, this.PART_TextEditor.Document.LineCount + 1));
}
private void OnAddMultiLineText(object sender, RoutedEventArgs e)
{
for (int i = 0; i < 250; i++)
{
this.PART_TextEditor.Document.Insert(
this.PART_TextEditor.Document.TextLength,
string.Format(
"----------------------------------------- Very long line #{1} -----------------------------------------{0}",
Environment.NewLine,
this.PART_TextEditor.Document.LineCount));
}
}
private void OnClearText(object sender, RoutedEventArgs e)
{
this.PART_TextEditor.Document.Remove(0, this.PART_TextEditor.Document.TextLength);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment