如何将 WPF TextBlock 绑定到文本文件?我想让 TextBlock 显示文件的内容。
问问题
7019 次
3 回答
2
您需要将文件读入内存中的字符串并绑定到该字符串。
查看型号:
class ViewModel
{
public string FileText { get; set; }
public void ReadFile(string path)
{
FileText = File.ReadAllText(path);
}
}
XAML:
<TextBlock Text="{Binding FileText}"/>
于 2010-01-03T15:47:42.100 回答
0
如果您希望将文本格式化为我的内联标记,您可以查看我在此处创建的 TextBlock 的子类。在 xaml 标记字符串和 InlineCollection(实际上是内联的通用列表)之间也有一个转换器。
于 2011-04-08T09:17:17.810 回答
0
这篇文章描述了一个自定义标记扩展,一旦定义,您就可以通过 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>
于 2016-12-21T11:16:49.697 回答