繁体   English   中英

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

[英]How can I access a control in WPF from another class or window

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

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

要访问其他WPF表单中的控件,您必须将该控件声明为public。 WPF中控件的默认声明是public,但您可以使用以下代码指定它:

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

之后,您可以搜索应用程序中的所有活动窗口,以查找具有以下控件的窗口:

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

不幸的是,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时,我也在努力解决这个问题。 但是,我找到了一个很好的方法,类似于老式的胜利形式方法(编写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中有关对象可访问性的命名。

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

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

targetWindow.ABUTTON.Background = Brushes.Transparent;

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

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

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

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

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

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

这可能是一个稍微不同的答案,但让我们考虑一下为什么我们需要在表单之间传递数据。 显然,原因是“可视化”。

使用委托或事件。

无需将元素声明为 Public 只是为了使其可见。 只需要能够在有限的基础上使用委托来转换窗口内的元素。

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

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

好编程

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM