Scottintexas
Well-known member
There are a few examples of binding to a RichTextbox on the internet. I searched all over and it comes down to two or three examples. I chose one of the examples to work with as there were a number of people that claimed success and it did not require installing new tools (which, for security reasons, I cannot do). The idea is to attach a bindable dependency property to a rich text box and bind the property with the text to it. So that's what I did, but when I run the program the dependency property never gets hit. When the program starts a break point in the dependency property demonstrates that the dependency property is getting registered. But when you set a string to the property in the view model, nothing happens. The dependency property never gets called.
Here is the Dependency Property
In the MainWindowViewModel I have this property.
And in the View I have this
I don't know what is missing. I cannot ask the people who originally posted their solutions. So, hopefully, one of the geniuses here can tell how to make it work.
Thank you
Here is the Dependency Property
C#:
namespace VBADeconstructor.View_Model
{
public class RichTextBoxHelper : DependencyObject
{
private static HashSet<Thread> _recursionProtection=new HashSet<Thread>();
public static string GetDocumentXaml(DependencyObject obj)
{
return (string)obj.GetValue(DocumentXamlProperty);
}
public static void SetDocumentXaml(DependencyObject obj, string value)
{
_recursionProtection.Add(Thread.CurrentThread);
obj.SetValue(DocumentXamlProperty, value);
_recursionProtection.Remove(Thread.CurrentThread);
}
public static readonly DependencyProperty DocumentXamlProperty = DependencyProperty.RegisterAttached
(
"DocumentXaml",
typeof(string),
typeof(RichTextBoxHelper),
new FrameworkPropertyMetadata
(
"",FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.AffectsRender,
(obj,e )=>
{
if(_recursionProtection.Contains(Thread.CurrentThread)) return;
var richTextBox=(RichTextBox)obj;
try
{
string docXml = GetDocumentXaml(richTextBox);
var stream = new MemoryStream(Encoding.UTF8.GetBytes(docXml));
FlowDocument doc;
if (!string.IsNullOrEmpty(docXml))
{
doc= (FlowDocument)XamlReader.Load(stream);
}
else
{
doc = new FlowDocument();
}
richTextBox.Document = doc;
}
catch (Exception)
{
richTextBox.Document = new FlowDocument();
}
richTextBox.TextChanged += (obj2, e2) =>
{
RichTextBox richbox = obj2 as RichTextBox;
if(richbox != null)
{
SetDocumentXaml(richTextBox,XamlWriter.Save(richbox.Document));
}
};
}
)
);
}
}
In the MainWindowViewModel I have this property.
C#:
public string Procedure
{
get { return _procedure; }
set
{
_procedure = value;
RaisePropertyChanged();
}
}
And in the View I have this
C#:
<RichTextBox Grid.Row="3"
Grid.Column="0"
Grid.ColumnSpan="2"
vm:RichTextBoxHelper.DocumentXaml="{Binding vm:MainWindowViewModel.Procedure, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
I don't know what is missing. I cannot ask the people who originally posted their solutions. So, hopefully, one of the geniuses here can tell how to make it work.
Thank you