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

后端 未结 7 1357
执笔经年
执笔经年 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:18

    To extend on what Nathan said, I used a safe cast:

    (System.Windows.Application.Current.MainWindow as MainWindow)?.TextBlock1.Text = "Setting Text from My Program";
    

    Note the comments on the answer Nathan gave. This isn't ideal but it works.

提交回复
热议问题