WinRt:将RTF字符串绑定到RichEditBox
本文关键字:绑定 RichEditBox 字符串 RTF WinRt | 更新日期: 2023-09-27 18:27:39
搜索了很长时间,将一些RTF文本绑定到Windows应用商店应用程序上的RichEditBox控件。甚至它也应该在TwoMay绑定模式下运行。…
。。。最后我找到了以下解决方案。我使用DependencyProperty RtfText从原始RichEditBox控件创建了一个继承的控件。
public class RichEditBoxExtended : RichEditBox
{
public static readonly DependencyProperty RtfTextProperty =
DependencyProperty.Register(
"RtfText", typeof (string), typeof (RichEditBoxExtended),
new PropertyMetadata(default(string), RtfTextPropertyChanged));
private bool _lockChangeExecution;
public RichEditBoxExtended()
{
TextChanged += RichEditBoxExtended_TextChanged;
}
public string RtfText
{
get { return (string) GetValue(RtfTextProperty); }
set { SetValue(RtfTextProperty, value); }
}
private void RichEditBoxExtended_TextChanged(object sender, RoutedEventArgs e)
{
if (!_lockChangeExecution)
{
_lockChangeExecution = true;
string text;
Document.GetText(TextGetOptions.None, out text);
if (string.IsNullOrWhiteSpace(text))
{
RtfText = "";
}
else
{
Document.GetText(TextGetOptions.FormatRtf, out text);
RtfText = text;
}
_lockChangeExecution = false;
}
}
private static void RtfTextPropertyChanged(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var rtb = dependencyObject as RichEditBoxExtended;
if (rtb == null) return;
if (!rtb._lockChangeExecution)
{
rtb._lockChangeExecution = true;
rtb.Document.SetText(TextSetOptions.FormatRtf, rtb.RtfText);
rtb._lockChangeExecution = false;
}
}
}
这个解决方案对我有效,也许对其他人也有效。:-)
已知问题:VirtualizingStackPanel中的奇怪行为。VirtualizationMode="Recycling"