在可绑定的RichTexBox mvvm wpf中加载rtf [英] Load rtf in bindable RichTexBox mvvm wpf

查看:56
本文介绍了在可绑定的RichTexBox mvvm wpf中加载rtf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是mvvm的新手,我想使用mvvm将rtf文件加载到RichTextBox中,但是文本似乎没有显示在我的richtextbox中.尝试在ViewModel中放置命令时,RichTextBox看起来非常复杂.我不确定哪里出问题了.

I'm new to mvvm and I would like to load an rtf file in a RichTextBox using mvvm, but the text doesn't seem to display in my richtextbox. Looks like RichTextBox is pretty complex to deal with when trying to place the commands in the ViewModel. I'm not sure where I go wrong.

ViewModel

ViewModel

 FlowDocument _script;
 public FlowDocument Script 
    {
        get { return _script; }
        set { _script = value; RaisePropertyChanged("Script"); }
    }
.
.
.
 private void LoadScript()
    {
        openFile.InitialDirectory = "C:\\";

        if (openFile.ShowDialog() == true)
        {
            string originalfilename = System.IO.Path.GetFullPath(openFile.FileName);

            if (openFile.CheckFileExists)
            {
                Script = new FlowDocument();
                TextRange range = new TextRange(Script.ContentStart, Script.ContentEnd);
                FileStream fStream = new FileStream(originalfilename, System.IO.FileMode.OpenOrCreate);
                range.Load(fStream, DataFormats.Rtf);
                fStream.Close();
            }  
         }
    }

视图

DataContext="{Binding ScriptBreakdownViewModel, Source={StaticResource Locator}}">

<Grid>
    <RichTextBox
        Local:RichTextBoxHelper.DocumentRtf="{Binding Script}"
        x:Name="rtfMain"
        HorizontalAlignment="Left"
        Width="673"
        VerticalScrollBarVisibility="Visible"
        Margin="0,59,0,10.4"
        />

RichTextBoxHelper

the RichTextBoxHelper

public class RichTextBoxHelper : DependencyObject
{
    public static string GetDocumentRtf(DependencyObject obj)
    {
        return (string)obj.GetValue(DocumentRtfProperty);
    }
    public static void SetDocumentRtf(DependencyObject obj, string value)
    {
        obj.SetValue(DocumentRtfProperty, value);
    }
    public static readonly DependencyProperty DocumentRtfProperty =
      DependencyProperty.RegisterAttached(
        "DocumentRtf",
        typeof(string),
        typeof(RichTextBoxHelper),
        new FrameworkPropertyMetadata
        {
            BindsTwoWayByDefault = true,
            PropertyChangedCallback = (obj, e) =>
            {
                var richTextBox = (RichTextBox)obj;

               //  Parse the XAML to a document (or use XamlReader.Parse())
                var Rtf = GetDocumentRtf(richTextBox);
                var doc = new FlowDocument();
                var range = new TextRange(doc.ContentStart, doc.ContentEnd);

                range.Load(new MemoryStream(Encoding.UTF8.GetBytes(Rtf)),
                  DataFormats.Rtf);

               //  Set the document
                richTextBox.Document = doc;

               //  When the document changes update the source
                range.Changed += (obj2, e2) =>
                {
                    if (richTextBox.Document == doc)
                    {
                        MemoryStream buffer = new MemoryStream();
                        range.Save(buffer, DataFormats.Rtf);
                        SetDocumentRtf(richTextBox,
                          Encoding.UTF8.GetString(buffer.ToArray()));
                    }
                };
            }
        });
}

和模型

  FlowDocument _script;
  public FlowDocument Script   // the Name property
    {
        get { return _script; }
        set { _script = value; NotifyPropertyChanged("Script"); }
    }

推荐答案

我在周末玩了一点.您的 RichTextBoxHelper 未被调用,因为类型错误.如果将其修改为如下所示:

I played with this a little more at the weekend. Your RichTextBoxHelper is not being called, because the types are wrong. If it is modified to look like this:

public class RichTextBoxHelper : DependencyObject
{
    public static FlowDocument GetDocumentRtf(DependencyObject obj)
    {
        return (FlowDocument)obj.GetValue(DocumentRtfProperty);
    }
    public static void SetDocumentRtf(DependencyObject obj, FlowDocument value)
    {
        obj.SetValue(DocumentRtfProperty, value);
    }
    public static readonly DependencyProperty DocumentRtfProperty =
      DependencyProperty.RegisterAttached(
        "DocumentRtf",
        typeof(FlowDocument),
        typeof(RichTextBoxHelper),
        new FrameworkPropertyMetadata
        {
            BindsTwoWayByDefault = true,
            PropertyChangedCallback = (obj, e) =>
            {
                var richTextBox = (RichTextBox)obj;
                richTextBox.Document = e.NewValue as FlowDocument;
            }
        });
}

然后将其选中,并正确设置RichTextBox Document属性.

Then it gets hit, and sets the RichTextBox Document property properly.

这篇关于在可绑定的RichTexBox mvvm wpf中加载rtf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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