How can I access one window's control (richtextbox) from another window in wpf?

后端 未结 4 879
感情败类
感情败类 2020-12-09 12:53

I\'m sure this is something very simple but I can\'t figure it out. I\'ve searched here and on msdn and have been unable to find the answer. I need to be able to set the ric

4条回答
  •  暖寄归人
    2020-12-09 13:31

    Application.Current contains a collection of all windows in you application, you can get the other window with a query such as

    var window2 = Application.Current.Windows
        .Cast()
        .FirstOrDefault(window => window is Window2) as Window2;
    

    and then you can reference the control from your code, as in

    var richText = window2.MyRichTextBox
    

提交回复
热议问题