1- using System . Windows . Threading ;
1+ using System . Threading ;
2+ using System . Windows . Threading ;
23using Microsoft . Xaml . Behaviors ;
34
45namespace MaterialDesignThemes . Wpf . Behaviors ;
@@ -11,18 +12,22 @@ public class TextBoxLineCountBehavior : Behavior<TextBox>
1112 private void AssociatedObjectOnTextChanged ( object sender , TextChangedEventArgs e ) => UpdateAttachedProperties ( ) ;
1213 private void AssociatedObjectOnLayoutUpdated ( object ? sender , EventArgs e ) => UpdateAttachedProperties ( ) ;
1314
15+ private int _uiUpdateInProgress = 0 ;
16+
1417 private void UpdateAttachedProperties ( )
1518 {
16- if ( AssociatedObject is { } associatedObject )
19+ if ( AssociatedObject is { } associatedObject &&
20+ Interlocked . CompareExchange ( ref _uiUpdateInProgress , 1 , 0 ) == 0 )
1721 {
1822 associatedObject . Dispatcher
19- . BeginInvoke ( ( ) =>
20- {
21- int lineCount = associatedObject . LineCount ;
22- associatedObject . SetCurrentValue ( TextFieldAssist . TextBoxLineCountProperty , lineCount ) ;
23- associatedObject . SetCurrentValue ( TextFieldAssist . TextBoxIsMultiLineProperty , lineCount > 1 ) ;
24- } ,
25- DispatcherPriority . Background ) ;
23+ . BeginInvoke ( ( ) =>
24+ {
25+ int lineCount = associatedObject . LineCount ;
26+ associatedObject . SetCurrentValue ( TextFieldAssist . TextBoxLineCountProperty , lineCount ) ;
27+ associatedObject . SetCurrentValue ( TextFieldAssist . TextBoxIsMultiLineProperty , lineCount > 1 ) ;
28+ Interlocked . CompareExchange ( ref _uiUpdateInProgress , 0 , 1 ) ;
29+ } ,
30+ DispatcherPriority . Background ) ;
2631 }
2732 }
2833
@@ -35,10 +40,10 @@ protected override void OnAttached()
3540
3641 protected override void OnDetaching ( )
3742 {
38- if ( AssociatedObject != null )
43+ if ( AssociatedObject is { } associatedObject )
3944 {
40- AssociatedObject . TextChanged -= AssociatedObjectOnTextChanged ;
41- AssociatedObject . LayoutUpdated -= AssociatedObjectOnLayoutUpdated ;
45+ associatedObject . TextChanged -= AssociatedObjectOnTextChanged ;
46+ associatedObject . LayoutUpdated -= AssociatedObjectOnLayoutUpdated ;
4247 }
4348 base . OnDetaching ( ) ;
4449 }
0 commit comments