1

WPF TextBlock をテキスト ファイルにバインドするにはどうすればよいですか? TextBlock にファイルの内容を表示させたい。

4

3 に答える 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 に答える