I try solve this problem. I have bindable richTextBox for Text and Document properties, this control is derived from richTextBox from Extended WPF Toolkit.
My scenario is : User is typing some text to richTextBox, if he wrote text emoticons I want convert this text emoticons to image emoticons and show him in richTextBox.
For example: Hello world :) -> Hello world IMAGE_SMILE
I have own class which convert string with emoticons to type of Paragraph with text and images.
Here ist it:
public Paragraph ConvertToMessageWithEmoticons(string rpText){...}
In WPF app I bind properties on Text and Document properties of richTextBox control.
//bind on Text property of richTextBoxControl
public string RtbText
{
get { return _rtbText; }
set
{
_rtbText = value;
NotifyPropertyChanged("RtbText");
}
}
//bind on Document property of richTextBoxControl
public FlowDocument RtbFlowDocument
{
get { return _rtbFlowDocument; }
set
{
_rtbFlowDocument = value;
NotifyPropertyChanged("RtbFlowDocument");
}
}
I use Reactive Extensions for .NET (Rx) and make Observer on property RtbText
Observable.FromEvent<PropertyChangedEventArgs>(this, "PropertyChanged")
.Where(e => e.EventArgs.PropertyName == "RtbText")
.Select(_ => this.RtbText)
.Where(text => text.Length > 1)
.Do(AddSmiles)
.Throttle(TimeSpan.FromSeconds(500))
.Subscribe(AddSmiles);
My problem is how elegant replace text with smiles to text with emoticons. I try this:
private void AddSmiles(string text)
{
Paragraph paragraph = _smileConverter.ConvertToMessageWithEmoticons(RtbText);
RtbFlowDocument.Blocks.Clear();
RtbFlowDocument.Blocks.Add(paragraph);
}
I try check on smiles only 3 last characters of RtbText, but if is user typing property RtbText is still change.
Any advice how replace text to text with smile during user typing?