When the RichEditBox text changes (including rich text changes), it updates the PlainText attached property using (TextGetOptions.None, out text) and then o.SetValue(PlainTextProperty, text).When the PlainText attached property is changed, it updates the RichEditBox with the plain text using (TextSetOptions.None, text).
RICH TEXT VS PLAIN TEXT CODE
The attached property basically does two things, but there's a lot of boilerplate code and state machinery surrounding it: Return (o.GetValue(RichEditChangeStateHelperProperty) Static RichEditChangeState GetState(DependencyObject o) Helper to get the state managed by the textbox (o.GetValue(RichEditChangeStateHelperProperty)Īs RichEditChangeStateHelper).State = state
Static void SetState(DependencyObject o, RichEditChangeState state) Helper to set the state managed by the textbox SetState(o, RichEditChangeState.RichTextChanged) Ĭase RichEditChangeState.PlainTextChanged: To avoid re-entrancy, make sure we're not already changing RichEdit.TextChanged += (sender, args) => O.SetValue(RichEditChangeStateHelperProperty, helper) Var helper = new RichEditChangeStateHelper() If (o.GetValue(RichEditChangeStateHelperProperty) != null) Static void AttachRichEditBoxChangingHelper(DependencyObject o) Inject our state into the textbox, and also attach an event-handler Typeof(RichEditChangeStateHelper), typeof(RichEditBo圎xtension), null) Static readonly DependencyProperty RichEditChangeStateHelperProperty =ĭependencyProperty.RegisterAttached("RichEditChangeStateHelper", the normal GetXXX and SetXXX static methods. Because this isn't used in XAML, we don't need Private attached property (never seen in XAML or anywhere else) to attach whether it is already being changed by code or not Helper class that just stores a state inside a textbox, determining Trivial state machine to determine who last changed the text properties (Windows.UI., text) Ĭase RichEditChangeState.RichTextChanged: SetState(o, RichEditChangeState.PlainTextChanged) Document if the PlainText was modified (vs. To avoid endless property updates, we make sure we only change the RichText's ensuring that we're made aware of any changes This attaches an event handler for the TextChange event in the RichEditBox, Private static void OnPlainTextChanged(DependencyObject o, Public static void SetPlainText(DependencyObject o, string s) Return o.GetValue(PlainTextProperty) as string Public static string GetPlainText(DependencyObject o) Typeof(RichEditBo圎xtension), new PropertyMetadata(null, OnPlainTextChanged)) Public static readonly DependencyProperty PlainTextProperty =ĭependencyProperty.RegisterAttached("PlainText", typeof(string), It mimics the "Text" property of normal text boxes This can be in its own file or again just pasted into the code-behind file. The implementation of RichEditBo圎xtension.PlainText is pretty straight-forward, but it takes a fair amount of code due to the dependency-property infrastructure and the need to avoid endless property updates (where changing the rich text triggers the plain text, which triggers the rich text which triggers the plain text, and so on and so on). There is another TextBlock on the page to show the current value of the PlainText property, and a few buttons to manipulate the text. The RichEditBox uses the attached property RichEditBo圎xtension.PlainText and binds it to the ViewModel property PlainText. Propert圜hanged?.Invoke(this, new Propert圜hangedEventArgs(propertyName)) Void RaisePropert圜hanged( string propertyName = "") Public event Propert圜hangedEventHandler Propert圜hanged public class ViewModel : INotifyPropert圜hanged You can put this in its own file, or paste it into the main code-behind file. Nothing special just a single string property. Private void MakeBold(object sender, RoutedEventArgs e)
Selection.MoveStart(TextRangeUnit.Word, 1) (TextSetOptions.None, "Here is some rich text") Private void ChangeRichText(object sender, RoutedEventArgs e) Model.PlainText = "Here is some plain text" Private void ChangeText(object sender, RoutedEventArgs e) This assumes you're using the default change the constructor name as appropriate public MainPage() Here is an example XAML page, code-behind, and ViewModel showing the usage of the attached property: XAMLĬopy this as the content of a new page in your project This attached property handles the conversion between the rich text and the plain text of the document. It is possible to data-bind the plain-text of a RichEditBox by using a custom attached property.