如何从另一个类或窗口访问WPF中的控件

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

在Windows窗体应用程序中它很容易,您可以将该控件的修饰符设置为True,并且您可以从该mainWindow的实例到达该控件,但在WPF中,我无法声明公共控件。 我怎样才能做到这一点?

对于其他WPF窗口中的访问控制,您必须将该控件声明为public,WPF中的控件的默认声明是公共的,您可以使用以下代码指定它:

 

之后你可以搜索应用程序中的所有活动窗口来查找,而不是像这样的控件:

 foreach (Window window in Application.Current.Windows) { if (window.GetType() == typeof(Window1)) { (window as Window1).textBox1.Text = "I change it from another windows"; } } 

不幸的是,WPF的基础是数据绑定。 以任何其他方式这样做是“反对谷物”,这是一种不好的做法,并且编码和理解通常更复杂。

对于您手头的问题,如果您要在视图之间共享数据(即使它只是一个视图),请创建一个视图模型类,其中包含表示数据的属性,并从视图中绑定到属性。

在您的代码中,仅管理您的视图模型类,而不是通过其可视控件和可视组合触摸实际视图。

我发现在WPF中,你必须将Window转换为MainWindow。

看起来很复杂,但很容易! 但是,也许不是最佳做法。

假设我们在MainWindow中有一个Label1,一个Button1,并且你有一个类来处理与用户界面相关的任何东西,称为UI。

我们可以拥有以下内容:

MainWindow类:

 namespace WpfApplication1 { public partial class MainWindow : Window { UI ui = null; //Here, "null" prevents an automatic instantiation of the class, //which may raise a Stack Overflow Exception or not. //If you're creating controls such as TextBoxes, Labels, Buttons... public MainWindow() { InitializeComponent(); //This starts all controls created with the XAML Designer. ui = new UI(); //Now we can safely create an instantiation of our UI class. ui.Start(); } } } 

UI类:

 namespace WpfApplication1 { public class UI { MainWindow Form = Application.Current.Windows[0] as MainWindow; //Bear in mind the array! Ensure it's the correct Window you're trying to catch. public void Start() { Form.Label1.Content = "Yay! You made it!"; Form.Top = 0; Form.Button1.Width = 50; //Et voilá! You have now access to the MainWindow and all it's controls //from a separate class/file! CreateLabel(text, count); //Creating a control to be added to "Form". } private void CreateLabel(string Text, int Count) { Label aLabel = new Label(); aLabel.Name = Text.Replace(" ", "") + "Label"; aLabel.Content = Text + ": "; aLabel.HorizontalAlignment = HorizontalAlignment.Right; aLabel.VerticalAlignment = VerticalAlignment.Center; aLabel.Margin = new Thickness(0); aLabel.FontFamily = Form.DIN; aLabel.FontSize = 29.333; Grid.SetRow(aLabel, Count); Grid.SetColumn(aLabel, 0); Form.MainGrid.Children.Add(aLabel); //Haha! We're adding it to a Grid in "Form"! } } } 

当我开始使用WPF时,我也在努力解决这个问题。 但是,我找到了一个很好的方法,类似于老式的胜利forms方法(编写VB.NET,对不起)。 添加前面说过的内容:

要直接更改活动窗口的模块或不同类的对象属性,请执行以下操作:

 Public Class Whatever Public Sub ChangeObjProperties() ' Here the window is indexed in case of multiple instances of the same ' window could possibly be open at any given time.. otherwise just use 0 Dim w As MainWindow = Application.Current.Windows(0) w.Button1.Content = "Anything" End Sub End Class 

您显然必须在代码中调用ChangeObjProperties()之前实例化Whatever

此外,无需担心XAML中有关对象可访问性的命名。

只需声明您的控件,使其公开:

  

然后,您可以从另一个控件访问它。

控件的默认声明是非公开的,内部的而非公开的!

因此允许从同一组件内访问控件。 如果要从另一个程序集访问wpf表单上的控件,则必须使用修饰符属性x:FieldModifier =“public”或使用Jean提出的方法。

 var targetWindow = Application.Current.Windows.Cast().FirstOrDefault(window => window is principal) as principal; targetWindow .BssAcesso.Background = Brushes.Transparent; 

只需从当前窗口调用它的任何控件:

 targetWindow.ABUTTON.Background = Brushes.Transparent; 

如何从wpf中的另一个窗口访问一个窗口的控件(richtextbox)?

访问另一个窗口中的任何控件非常简单。 让我们说从登录窗口访问MainWindow。 以下是步骤:

 MainWindow MW = new MainWindow(); //declare the mainwindow MW.Label1.Content = "Hello world"; //specify what control MW.ShowDialog(); //check what happen to that control 

好编程