In my view model I have a byte[] (called "DocumentBytes") that is read from a .docx file. I have my view set up like this:
The rich textbox correctly displays the document from the file. When the user makes a change to the content of the rich textbox and then the textbox loses focus, I need the byte[] (DocumentBytes) to be written back with the updated contents of the textbox, and in the docx format, as it was provided to the DocxDataProvider.
Out-of-the-box this didn't seem to work. I expected the data provider to just write the value back to my property, but it didn't do that.
So, I tried to implement this myself using my view's code behind:
This caused problems and threw exceptions when I would edit the document's content in the rich textbox, and then click on back on it (the LostFocus event was firing each time I clicked on the rich textbox).
I'm really surprised there isn't a short and concise example in the WPF documentation on this, and it's something I imagine most people have to do. Perhaps the documentation is there and I just haven't found it.
So, what I'm looking for is a example of how to actually bind a byte[] from my view model, and get the view to write back to that same property when the rich textbox's content is updated.
<telerik:DocxDataProvider x:Name="docxDataProvider" Docx="{Binding DocumentBytes, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" RichTextBox="{Binding ElementName=SnippetPreviewRichTextBox}" /><telerik:RadRichTextBox x:Name="SnippetPreviewRichTextBox"/>The rich textbox correctly displays the document from the file. When the user makes a change to the content of the rich textbox and then the textbox loses focus, I need the byte[] (DocumentBytes) to be written back with the updated contents of the textbox, and in the docx format, as it was provided to the DocxDataProvider.
Out-of-the-box this didn't seem to work. I expected the data provider to just write the value back to my property, but it didn't do that.
So, I tried to implement this myself using my view's code behind:
private bool DocumentHasChanged { get; set; }private void SnippetPreviewRichTextBox_GotFocus(object sender, RoutedEventArgs e){ this.DocumentHasChanged = false;}private void SnippetPreviewRichTextBox_LostFocus(object sender, RoutedEventArgs e){ if (!this.DocumentHasChanged) return; var viewModel = this.DataContext as SnippetPreviewViewModel; if (viewModel == null) return; var formatProvider = new DocxFormatProvider(); var docXBytes = formatProvider.Export(this.SnippetPreviewRichTextBox.Document); viewModel.DocumentBytes = docXBytes;}private void SnippetPreviewRichTextBox_DocumentContentChanged(object sender, EventArgs e){ this.DocumentHasChanged = true;}This caused problems and threw exceptions when I would edit the document's content in the rich textbox, and then click on back on it (the LostFocus event was firing each time I clicked on the rich textbox).
I'm really surprised there isn't a short and concise example in the WPF documentation on this, and it's something I imagine most people have to do. Perhaps the documentation is there and I just haven't found it.
So, what I'm looking for is a example of how to actually bind a byte[] from my view model, and get the view to write back to that same property when the rich textbox's content is updated.