简体   繁体   English

使用MVVM将RichTextBox的内容保存到文件

[英]Save RichTextBox's content to file using MVVM

I have a problem with RichTextBox and I would like to save the content of the document in a text file. 我的RichTextBox有问题,我想将文档的内容保存在文本文件中。 For that I use the next code: 为此,我使用下一个代码:

XAML

<RichTextBox Grid.Row="0" x:Name="myRichTextBox" AcceptsTab="True" Margin="20">
    <FlowDocument>
        <Paragraph>
            <Run>Some Paragraph</Run>
        </Paragraph>
    </FlowDocument>
</RichTextBox>

Code

private void btnSaveToTxt_Click(object sender, RoutedEventArgs e)
{
    string fileName = @"D:\testRichTextBox1Text.txt";
    SaveToTextFile(fileName);

    MessageBox.Show("Text File Saved");
}

public void SaveToTextFile(string fileName)
{
    TextRange range;
    FileStream fileStream;

    range = new TextRange(myRichTextBox.Document.ContentStart,
                              myRichTextBox.Document.ContentEnd);

    fileStream = new FileStream(fileName, FileMode.Create);
    range.Save(fileStream, DataFormats.Text);

    fileStream.Close();
}

This code is Ok and it works, but how would I do this with MVVM . 这段代码还可以,并且可以正常工作,但是我将如何使用MVVM做到这一点。 For this approach I need the RichTextBox's x:Name="myRichTextBox" property. 对于这种方法,我需要RichTextBox的x:Name =“ myRichTextBox”属性。 I was thinking to bind a ICommand to invoke SaveToTextFile() method, but without the Name property from RichTextBox it wont work. 我当时想绑定一个ICommand来调用SaveToTextFile()方法,但是如果没有RichTextBox的Name属性,它将无法正常工作。

Is there a way to do this with MVVM ? 有没有办法用MVVM做到这一点? Thanks! 谢谢!

The viewmodel don't need the Name property. 该视图模型不需要Name属性。 To save the document it just need the FlowDocument object. 要保存文档,只需要FlowDocument对象。 So create a command for Save operation and pass the FlowDocument instance through CommandParameter. 因此,请创建一个用于Save操作的命令,并通过CommandParameter传递FlowDocument实例。

public class ViewModel 
{
    string fileName = @"D:\testRichTextBox1Text.txt";

    private ICommand saveCommand;

    public ICommand SaveCommand
    {
        get
        {
            if (saveCommand == null)
            {
                saveCommand = new DelegateCommand(SaveToTextFile);
            }
            return saveCommand;
        }
    }

    public void SaveToTextFile(object document)
    {
        TextRange range;
        FileStream fileStream;

        range = new TextRange(((FlowDocument)document).ContentStart,
                              ((FlowDocument)document).ContentEnd);

        fileStream = new FileStream(fileName, FileMode.Create);
        range.Save(fileStream, DataFormats.Text);
        fileStream.Close();
        MessageBox.Show("Text File Saved");
    }
}

XAML looks like below, XAML如下所示,

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition />
    </Grid.RowDefinitions>
    <Button Content="Save" Margin="20 10" Command="{Binding SaveCommand}" CommandParameter="{Binding ElementName=myRichTextBox, Path=Document}"/>
    <RichTextBox Grid.Row="1" x:Name="myRichTextBox" AcceptsTab="True" Margin="20">
        <FlowDocument>
            <Paragraph>
                <Run>Some Paragraph</Run>
            </Paragraph>
        </FlowDocument>
    </RichTextBox>
</Grid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM