/ / WPF TextBlockをテキストファイルにバインド-wpf、data-binding、text-files、textblock

WPF TextBlockをテキストファイルにバインドする - wpf、データバインディング、テキストファイル、テキストブロック

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

回答:

回答№1は2

ファイルをメモリ内の文字列に読み込み、代わりにその文字列にバインドする必要があります。

モデルを表示する:

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

XAML:

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

回答№2の場合は0

テキストをインラインマークアップでフォーマットする場合は、作成したTextBlockのサブクラスを見ることができます。 ここに。 xamlマークアップの文字列とInlineCollection(実際にはInlinesの汎用リスト)の間にもコンバーターがあります。


回答№3の場合は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>