How to access WPF MainWindow Controls from my own .cs file

后端 未结 7 1186
执笔经年
执笔经年 2021-01-31 18:28

I am a NOVICE and am very much struggling with what seems should be a very simple task. How do I modify a property of a MainWindow TextBlock, from ano

7条回答
  •  无人及你
    2021-01-31 19:25

    Use MVVM pattern to access properties of the control and modify them:

    public class Student
    {
        public Student()
        {
        }
    
        public string Name
        {
            get { return "Setting Text from My Program"; }
        }
    }
    

    Set the DataContext of the XAML in the code behind:

    this.DataContext = new Student();
    

    Bind the Text property to Name:

    
    

提交回复
热议问题