2010-01-03 102 views

回答

2

您需要将文件读入内存中的字符串,并将其绑定到该字符串。

视图模型:

class ViewModel 
{ 
    public string FileText { get; set; } 
    public void ReadFile(string path) 
    { 
     FileText = File.ReadAllText(path); 
    } 
} 

XAML:

<TextBlock Text="{Binding FileText}"/> 
0

如果你想要的文字被格式化我行内标记,你可以看看子类的TextBlock的我做了here。在xaml标记字符串和InlineCollection(实际上是Inline的通用列表)之间也有一个转换器。

0

This post介绍,一旦定义的自定义标记扩展,让你包括通过XAML文件的内容:

<Window 
    x:Class="WPF.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:wpf="clr-namespace:WPF"> 
    <TextBlock Text="{wpf:Text 'Assets/Data.txt'}" /> 
</Window>