WinRt:将 RTF 字符串绑定到 RichEditBox [英] WinRt: Binding a RTF String to a RichEditBox

查看:15
本文介绍了WinRt:将 RTF 字符串绑定到 RichEditBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索了很长时间以将一些 RTF 文本绑定到 Windows 应用商店应用程序上的 RichEditBox 控件.即使它应该在 TwoMay 绑定模式下运行....

Searched a long time to bind some RTF text to an RichEditBox Control on Windows Store Applications. Even it should function in TwoMay Binding Mode. ...

推荐答案

... 最后我找到了以下解决方案.我使用 DependencyProperty RtfText 从原始 RichEditBox 控件创建了一个继承控件.

... finally I found the following solution. I created a inherited control from the original RichEditBox control with a DependencyProperty RtfText.

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;
        }
    }
}

此解决方案适用于我 - 也许对其他人也适用.:-)

This solution works for me - perhaps for others too. :-)

已知问题: VirtualizingStackPanel.VirtualizationMode="Recycling"

Known issues: strange behaviours in VirtualizingStackPanel.VirtualizationMode="Recycling"

这篇关于WinRt:将 RTF 字符串绑定到 RichEditBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆