Hello,
I got the requirement that we need to support the binding and HTML text in WPF Rich Text so I started creating an attached properties for that. But I'm facing a few problems. As Microsoft has XamlToHTML converter, the conversion can be done very easily. But having a few problems with LostFocus and PropertyChanged.
Problem #1:
Let me started with LostFocus problem.
My attached property is as below.
public static class RichTextboxAssistant{
public static readonly DependencyProperty BoundDocument = DependencyProperty.RegisterAttached("BoundDocument", typeof(string), typeof(RichTextboxAssistant), new PropertyMetadata(OnBoundDocumentChanged));
private static void OnBoundDocumentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){
RichTextBox box = d as RichTextBox;
if (box == null){
return;
}
box.LostFocus -= HandleTextChanged;
TextRange tr = new TextRange(box.Document.ContentStart,
box.Document.ContentEnd);
string newXAML = (string)e.NewValue;
MemoryStream xamlMemoryStream = new MemoryStream(Encoding.ASCII.GetBytes(newXAML));
ParserContext pc = new ParserContext();
pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
FlowDocument doc = new FlowDocument();
Section section = XamlReader.Load(xamlMemoryStream, pc) as Section;
doc.Blocks.Add(section);
box.Document = doc;
box.LostFocus += HandleTextChanged;
}
private static void HandleTextChanged(object sender, RoutedEventArgs e)
{
RichTextBox box = sender as RichTextBox;
TextRange tr = new TextRange(box.Document.ContentStart,
box.Document.ContentEnd);
MemoryStream ms = new MemoryStream();
tr.Save(ms, DataFormats.Xaml);
string xamlText = ASCIIEncoding.Default.GetString(ms.ToArray());
SetBoundDocument(box, xamlText);
}
public static string GetBoundDocument(DependencyObject dp)
{
return (string)dp.GetValue(BoundDocument);
}
public static void SetBoundDocument(DependencyObject dp, string value)
{
dp.SetValue(BoundDocument, value);
}
}
Usages
XAML
<RichTextBox Name="mainRTB" AcceptsTab="True"
asis:RichTextboxAssistant.BoundDocument="{Binding Document}"
Height="150" />
I set the Document property like that and it’s working fine.
Document = "<font color=\"red\">Michael" + new Random().Next().ToString() + " </font> <B>Sync</B>";
I also have two buttons that can set the text as above and get the Document from RichTextbox. I’m able to set/get the value by clicking the buttons.
The problem started when I type something in RichTextbox and I move to the another control.
I’m not able to set anything from Button Click. Any idea why it’s not working?
Problem #2:
If I change LostFocus to TextChanged Event in my attached property then I’m not able to set anything.
Any idea how to support LostFocus and TextChanged event with Rich Textbox?
Thanks in advance.
Regards,
Michael Sync
Michael, Josh is correct. There are days when boots are required. I can remember one day where a stream fisherman’s ½ body suit was required…