Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在可绑定的RichTexBox mvvm wpf中加载rtf_C#_Wpf_Mvvm_Binding_Richtextbox - Fatal编程技术网

C# 在可绑定的RichTexBox mvvm wpf中加载rtf

C# 在可绑定的RichTexBox mvvm wpf中加载rtf,c#,wpf,mvvm,binding,richtextbox,C#,Wpf,Mvvm,Binding,Richtextbox,我是mvvm新手,我想使用mvvm在RichTextBox中加载rtf文件,但我的RichTextBox中似乎没有显示文本。在试图将命令放置到ViewModel中时,RichTextBox看起来非常复杂。我不知道哪里出了问题 视图模型 FlowDocument _script; public FlowDocument Script { get { return _script; } set { _script = value; RaiseProper

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

视图模型

 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"
        />
模型呢

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

我试图填补上面示例代码中的空白,但您的依赖项属性从未被触发

相反,我通过稍微更改XAML来填充RichTextBox:

    <Button Height="30" Width="70" VerticalAlignment="Top" Command="{Binding OpenFileCommand}" CommandParameter="{Binding ElementName=myRichTextBox}">Load</Button>
    <RichTextBox Name="myRichTextBox"
        HorizontalAlignment="Left"
        Width="673"
        VerticalScrollBarVisibility="Visible"
        Margin="0,59,0,10.4"></RichTextBox>

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

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文档属性。

这对您有帮助吗?是否确实调用了ViewModel中的
LoadScript
方法?这比直接绑定到
RichTextBox.Document
属性更好吗?
public class OpenFileCommand : ICommand
{
    private OpenFileDialog openFile = new OpenFileDialog();

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
        var rtf = parameter as RichTextBox;
        rtf.Document = LoadScript();
    }

    public event EventHandler CanExecuteChanged;

    private FlowDocument LoadScript()
    {
        openFile.InitialDirectory = "C:\\";

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

            if (openFile.CheckFileExists)
            {
                var 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();
                return script;
            }
        }

        return null;
    }
}
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;
            }
        });
}