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

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

问题描述

搜索很长的时间来绑定某些RTF文本到RichEditBox控制在Windows商店的应用。即使它应该在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怪异的行为=回收

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

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

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