如何从 wpf 中的另一个窗口访问一个窗口的控件(富文本框)? [英] How can I access one window's control (richtextbox) from another window in wpf?

查看:153
本文介绍了如何从 wpf 中的另一个窗口访问一个窗口的控件(富文本框)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定这是非常简单的事情,但我无法弄清楚.我在这里和 msdn 上搜索过,但一直找不到答案.我需要能够通过richtextbox.Selection.Select(TextPointer1, Textpointer2)设置richtextboxes选择.

解决方案

Application.Current 包含你的应用程序中所有窗口的集合,你可以通过诸如

之类的查询来获取其他窗口>

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

然后你可以从你的代码中引用控件,如

var richText = window2.MyRichTextBox

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 richtextboxes selection via richtextbox.Selection.Select(TextPointer1, Textpointer2).

解决方案

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<Window>()
    .FirstOrDefault(window => window is Window2) as Window2;

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

var richText = window2.MyRichTextBox

这篇关于如何从 wpf 中的另一个窗口访问一个窗口的控件(富文本框)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆