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?
"???");