0

I want to do two way binding on RichEditBox. for this control I have to use Attached property, because there is no Text or similar properties for RichEditBox.

public static class RtfText
{
    public static string GetRichText(DependencyObject obj)
    {
        return (string)obj.GetValue(RichTextProperty);
    }

    public static void SetRichText(DependencyObject obj, string value)
    {
        obj.SetValue(RichTextProperty, value);
    }

    // Using a DependencyProperty as the backing store for RichText.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty RichTextProperty =
        DependencyProperty.RegisterAttached("RichText", typeof(string), typeof(RtfText), new PropertyMetadata(string.Empty, Callback));

    private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var reb = (RichEditBox)d;
        reb.Document.SetText(TextSetOptions.FormatRtf, (string)e.NewValue);
    }
}

doing the binding.

<RichEditBox local:RtfText.RichText="{Binding Inputs, Mode=TwoWay}"/>

in view model.

private string _inputs = string.Join(Environment.NewLine, new[] { "0, 0", "0, 1", "1, 0", "1, 1" });
public string Inputs
{
    get { return _inputs; }
    set { _inputs = value; NotifyOfPropertyChange(() => Inputs); }
}

but it doesn't work. initial values are put in rich edit box. but editing them has no effect on this property. what's the problem?


I want to stick with Caliburn micro and WinRT, please don't suggest other toolkits. thank you.


This answer gives some sights, but that's for PasswordBox. any Ideas?

ConventionManager.AddElementConvention<PasswordBox>(
    RtfText.RichTextProperty,
    "???", // what to put in here?
    "???");
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
  • I'm not sure how it fits into Caliburn, but your basic problem is that you're not registering for text changes. Since the basic problem has nothing to do with Caliburn, I wrote [a separate Q&A](https://stackoverflow.com/questions/49102980/how-can-i-databind-to-the-plain-text-value-of-a-richeditbox-in-uwp) - does it help? – Peter Torr - MSFT Mar 05 '18 at 03:14
  • Thanks for the answer. Currently i can not try this but ill check this out at night. @PeterTorr-MSFT – M.kazem Akhgary Mar 05 '18 at 06:14
  • @PeterTorr-MSFT thanks a lot. your attached property implementation worked like a charm. marking as duplicate. – M.kazem Akhgary Mar 08 '18 at 11:53

0 Answers0