代码之家  ›  专栏  ›  技术社区  ›  kjv

将WPF文本块绑定到文本文件

  •  1
  • kjv  · 技术社区  · 15 年前

    如何将WPF文本块绑定到文本文件?我希望文本块显示文件的内容。

    3 回复  |  直到 8 年前
        1
  •  2
  •   Aviad P.    15 年前

    您需要将文件读取到内存中的字符串中,然后绑定到该字符串。

    视图模型:

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

    XAML:

    <TextBlock Text="{Binding FileText}"/>
    
        2
  •  0
  •   Community CDub    7 年前

    如果您希望文本格式化我的内联标记,可以查看我创建的textBlock的子类。 here . XAML标记字符串和inlineCollection(实际上是一个一般的inline列表)之间也有一个转换器。

        3
  •  0
  •   Community CDub    7 年前

    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>