如何从其他类或窗口访问Windows wpf应用程序中的控件

问题描述:

大家好



我想在wpf中的mainWindow中访问我的控件,如按钮或文本框,但我不能这样做。

在Windows窗体应用程序中它很容易,你可以将该控件的修饰符设置为True,你可以从该mainWindow的实例到达该控件,但是在WPF中我不能声明一个公共控件,我怎么做?

Hi everyone

I want to access my controls like button or textbox in mainWindow in wpf, but I cant do this.
in windows form application its so easy, you can set modifier of that control to True and you can reach that control from instance of that mainWindow, but in WPF I can''t declare a pubic control, How ca I do this?

Just declare your control like this to make it public:

<TextBox x:Name="textBox1" x:FieldModifier="public" />

You can then access it from another control.