From 18d74e72b55f4e0981c50e49f9416aaefff5e6b9 Mon Sep 17 00:00:00 2001 From: brianlagunas_cp Date: Mon, 20 Jun 2011 14:13:43 +0000 Subject: [PATCH] RichTextBox: when the SetText was placed in the Dispatcher the TextChanged event would fire multiple times which would set the source value mutliple times. This has been fixed. --- .../RichTextBox/RichTextBox.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/RichTextBox.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/RichTextBox.cs index 0ff3d3cb..20a9a9ec 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/RichTextBox.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/RichTextBox.cs @@ -10,6 +10,7 @@ namespace Microsoft.Windows.Controls #region Private Members private bool _textSetInternally; + bool _surpressGetText; #endregion //Private Members @@ -46,13 +47,18 @@ namespace Microsoft.Windows.Controls // if the text is not being set internally then load the text into the RichTextBox if (!rtb._textSetInternally) { - if (!rtb._textSetInternally) - { - Dispatcher.CurrentDispatcher.BeginInvoke(new Action(delegate() + //to help with performance this is placed on the dispatcher for processing. For some reason when this is done the TextChanged event is fired multiple times + //forcing the UpdateText method to be called multiple times and the setter of the source property to be set multiple times. To fix this, we simply set the _surpressGetText + //member to true before the operation and set it to false when the operation completes. This will prevent the Text property from being set multiple times. + rtb._surpressGetText = true; + DispatcherOperation dop = Dispatcher.CurrentDispatcher.BeginInvoke(new Action(delegate() { rtb.TextFormatter.SetText(rtb.Document, (string)e.NewValue); }), DispatcherPriority.Background); - } + dop.Completed += (sender, ea) => + { + rtb._surpressGetText = false; + }; } } @@ -99,7 +105,8 @@ namespace Microsoft.Windows.Controls { _textSetInternally = true; - Text = TextFormatter.GetText(Document); + if (!_surpressGetText) + Text = TextFormatter.GetText(Document); _textSetInternally = false; }