Bind WPF TextBlock to text file
问题 How can I bind a WPF TextBlock to a text file? I want for the TextBlock to display the content of the file. 回答1: You need to read the file into a string in memory and bind to that string instead. View model: class ViewModel { public string FileText { get; set; } public void ReadFile(string path) { FileText = File.ReadAllText(path); } } XAML: <TextBlock Text="{Binding FileText}"/> 回答2: If you want the text to be formatted my inline markup you could look at the sub-class of TextBlock I made