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

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

    Because nobody else has actually answered the question I'm going to tell you how to achieve what you want, but do listen to the posters who said that in a real application you would use MVVM. However there are times when you need to do what you ask so the code you need is:

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

提交回复
热议问题