Phi*_*hil 5 c# wpf binding richtextbox mvvm
我是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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
风景
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"
/>
Run Code Online (Sandbox Code Playgroud)
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()));
}
};
}
});
}
Run Code Online (Sandbox Code Playgroud)
和模型
FlowDocument _script;
public FlowDocument Script // the Name property
{
get { return _script; }
set { _script = value; NotifyPropertyChanged("Script"); }
}
Run Code Online (Sandbox Code Playgroud)
我尝试填补上面示例代码中的空白,但您的依赖属性从未被触发。
相反,我通过稍微更改 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>
Run Code Online (Sandbox Code Playgroud)
有一个绑定到 的按钮OpenFileCommand,它是 ViewModel 上的一个属性。它将 RichTextBox 作为参数传递给命令本身。我已将该LoadScript()方法从 ViewModel 移至 Command。
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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2226 次 |
| 最近记录: |