@@ -34,16 +34,22 @@ namespace AvaloniaEdit.Editing
3434 /// </summary>
3535 public abstract class AbstractMargin : Control , ITextViewConnect
3636 {
37- static AbstractMargin ( )
37+ private TextArea _textArea ;
38+
39+ public AbstractMargin ( )
3840 {
39- TextViewProperty . Changed . Subscribe ( OnTextViewChanged ) ;
41+ this . GetObservableWithHistory ( TextViewProperty ) . Subscribe ( o =>
42+ {
43+ _wasAutoAddedToTextView = false ;
44+ OnTextViewChanged ( o . Item1 , o . Item2 ) ;
45+ } ) ;
4046 }
4147
4248 /// <summary>
4349 /// TextView property.
4450 /// </summary>
4551 public static readonly AvaloniaProperty < TextView > TextViewProperty =
46- AvaloniaProperty . Register < AbstractMargin , TextView > ( " TextView" ) ;
52+ AvaloniaProperty . Register < AbstractMargin , TextView > ( nameof ( TextView ) ) ;
4753
4854 /// <summary>
4955 /// Gets/sets the text view for which line numbers are displayed.
@@ -55,13 +61,6 @@ public TextView TextView
5561 set => SetValue ( TextViewProperty , value ) ;
5662 }
5763
58- private static void OnTextViewChanged ( AvaloniaPropertyChangedEventArgs e )
59- {
60- var margin = ( AbstractMargin ) e . Sender ;
61- margin . _wasAutoAddedToTextView = false ;
62- margin . OnTextViewChanged ( ( TextView ) e . OldValue , ( TextView ) e . NewValue ) ;
63- }
64-
6564 // automatically set/unset TextView property using ITextViewConnect
6665 private bool _wasAutoAddedToTextView ;
6766
@@ -110,6 +109,37 @@ protected virtual void OnTextViewChanged(TextView oldTextView, TextView newTextV
110109 }
111110
112111 TextViewDocumentChanged ( null , null ) ;
112+
113+ if ( oldTextView != null )
114+ {
115+ oldTextView . VisualLinesChanged -= TextViewVisualLinesChanged ;
116+ }
117+
118+ if ( newTextView != null )
119+ {
120+ newTextView . VisualLinesChanged += TextViewVisualLinesChanged ;
121+
122+ // find the text area belonging to the new text view
123+ _textArea = newTextView . GetService ( typeof ( TextArea ) ) as TextArea ;
124+ }
125+ else
126+ {
127+ _textArea = null ;
128+ }
129+ }
130+
131+ /// <summary>
132+ /// Called when the attached textviews visual lines change.
133+ /// Default behavior is to Invalidate Margins Visual.
134+ /// </summary>
135+ protected virtual void OnTextViewVisualLinesChanged ( )
136+ {
137+ InvalidateVisual ( ) ;
138+ }
139+
140+ private void TextViewVisualLinesChanged ( object sender , EventArgs e )
141+ {
142+ OnTextViewVisualLinesChanged ( ) ;
113143 }
114144
115145 private void TextViewDocumentChanged ( object sender , EventArgs e )
0 commit comments