[英]Save the text of a RichTextBox
I could do this easily in code-behind but don't want to break the MVVM
pattern. 我可以在代码隐藏中轻松地做到这一点,但不想破坏MVVM
模式。
I have a RichTextBox
and a 'Save' Button in my View. 我的视图中有一个RichTextBox
和一个“保存”按钮。 I want to save the contents of the RichTextBox to a file on the Save button click. 我想将RichTextBox的内容保存到单击“保存”按钮上的文件中。
I have written a delegate class which expects a method to return void and have a single object parameter. 我已经编写了一个委托类,该委托类期望一个方法返回void并具有单个对象参数。 I did this because I couldn't use the System.Action delegate. 我这样做是因为我无法使用System.Action委托。
class BtnCommandParameterised : ICommand
{
public delegate void ActionParameterised(object rtb);
private ActionParameterised _actionParameterised;
public object _object;
public BtnCommandParameterised(ActionParameterised BtnCommandParameterisedActionParameterised)
{
_actionParameterised = BtnCommandParameterisedActionParameterised;
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
_actionParameterised.Invoke(_object);
}
public event EventHandler CanExecuteChanged;
}
Then in my ViewModel I have an InitialiseBtnCommands()
method which is called from the constructor. 然后在我的ViewModel中,我有一个InitialiseBtnCommands()
方法,该方法从构造函数中调用。 This initializes the command: 这将初始化命令:
private void InitialiseBtnCommands()
{
ReturnTextAsStringCommand = new BtnCommandParameterised(ReturnTextAsStringCommandAction);
}
This invokes my method to save the file. 这将调用我的方法来保存文件。
private void ReturnTextAsStringCommandAction(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();
Xceed.Wpf.Toolkit.MessageBox.Show("Text File Saved");
}
Finally, in the View XAML, this is my binding: 最后,在View XAML中,这是我的绑定:
<Grid Name="MainGrid" DataContext="{StaticResource EditorViewModel}">
<xctk:RichTextBox SpellCheck.IsEnabled="True" HorizontalAlignment="Center" Margin="206,166,206,60" Name="richTextBoxArticleBody" AcceptsTab="True" BorderBrush="Silver" BorderThickness="1" VerticalAlignment="Center" Height="306" Width="600" FontFamily="Arial"/>
<Button Command="{Binding ReturnTextAsStringCommand}" CommandParameter="{Binding ElementName=richTextBoxArticleBody, Path=Document}" Content="Save Article Text" Height="23" HorizontalAlignment="Left" Margin="703,478,0,0" Name="button1" VerticalAlignment="Top" Width="103" />
</Grid>
The command is being called and my method is invoked but nothing is passed into the ReturnTextAsStringCommandAction
method? 正在调用命令并且调用了我的方法,但是什么都没有传递到ReturnTextAsStringCommandAction
方法中?
I'm new to MVVM, finding things shall we say, a little confusing. 我是MVVM的新手,发现一些令人困惑的事情,我们应该说。
You're passing _object into the invoke method. 您正在将_object传递到invoke方法中。 I think you're meaning to pass parameter. 我认为您是要传递参数。
public void Execute(object parameter)
{
_actionParameterised.Invoke(parameter);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.