Bind WPF TextBlock to text file

前端 未结 3 1655
执笔经年
执笔经年 2021-01-23 17:05

How can I bind a WPF TextBlock to a text file? I want for the TextBlock to display the content of the file.

3条回答
  •  梦毁少年i
    2021-01-23 17:23

    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:

    
    

提交回复
热议问题