如何从WPF中的另一个类操作窗口对象 [英] How to manipulate a window object from another class in WPF

查看:281
本文介绍了如何从WPF中的另一个类操作窗口对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF和C#新。我知道很多VB.NET和我习惯的方式,当我把一个表单对象文本框一样,等我从另一个窗体调用它。现在,我使用WPF,我很困惑。因为我有一个主窗口。我想从类在主窗口中添加和项目列表框。在VB.Net,它只是这样。

I'm new in WPF and C#. I know a lot of VB.NET and I'm used to the way when I call a form object like textboxes, etc. I'm calling it from another form. Now, I'm using WPF, I'm confused. Because I have a Main Window. And I want to add and item to a listbox in the Main Window from a Class. In VB.Net , its just like this.

    IN FORM2

    Form1.Textbox.Text = "";



其中,我不能做到这一点在WPF。是否有人可以帮助我。谢谢!

Wherein I can't do it in WPF. Can someone please Help me. Thanks!

推荐答案

在XAML中定义WPF窗口有自己的控制从其他类和形式公开,除非你专门与它们标记 X:。FieldModifier 属性为私有

WPF windows defined in XAML have their controls publicly accessible from other classes and forms, unless you specifically mark them with the x:FieldModifier attribute as private.

因此,如果你让你在其他类访问主窗口的一个实例,无论是窗口还是别的,你就可以从第二类中的填充控制。

Therefore, if you make an instance of your main window accessible in another class, be it a Window or anything else, you'll be able to populate controls from within this second class.

一个特殊情况是,当你想更新一个控制的内容,从已在它的上面开了一个子窗口的主窗口。就是这样一个情况下,你可以将子窗口的所有者属性设置为当前,主窗口,以访问它,而孩子是可见的。例如,假设您已经定义了这两个窗口:

A particular scenario is when you want to update the contents of a control in your main window from a child window that you have opened on top of it. Is such a case, you may set the child window's Owner property to the current, main window, in order to access it while the child is visible. For instance, let's say you have defined these two windows:

// MainWindow
<Window x:Class="TestApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox Name="mainListBox" Height="250" HorizontalAlignment="Stretch" VerticalAlignment="Top"/>
        <Button Content="Open Another Window" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="20" Click="OpenAnotherWindow_Click"/>
    </Grid>
</Window>

// AnotherWindow
<Window x:Class="TestApplication.AnotherWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="AnotherWindow" Height="300" Width="300">
    <Grid>
        <Button Content="Add New Item to Main Window" HorizontalAlignment="Center" VerticalAlignment="Center" Click="AddNewItem_Click"/>
    </Grid>
</Window>



在各自XAML文件。

each in its own XAML file.

主窗口的代码背后,按钮单击处理程序中,你看 AnotherWindow 的实例作为一个对话框,其所有者属性设置主窗口的实例:

In MainWindow's code behind, inside the button click handler, you show an instance of AnotherWindow as a dialog and set its Owner property to MainWindow's instance:

private void OpenAnotherWindow_Click(object sender, RoutedEventArgs e)
{
    AnotherWindow anotherWindow = new AnotherWindow();
    anotherWindow.Owner = this;

    anotherWindow.ShowDialog();
}

现在,您可以访问主窗口的从实例 AnotherWindow 的所有者的财产,以一个新的项目添加到的ListBox 后面 AnotherWindow 在其上定义,在按钮单击处理程序控件的代码:

Now, you can access the MainWindow's instance from AnotherWindow's Owner property, in order to add a new item to the ListBox control defined on it, in the button click handler in AnotherWindow's code behind:

private void AddNewItem_Click(object sender, RoutedEventArgs e)
{
     MainWindow mainWindow = Owner as MainWindow;

     mainWindow.mainListBox.Items.Add(new Random().Next(1000).ToString());
}



它只是增加了一个新的随机数到的ListBox ,以显示代码如何访问和主窗口修改控件的数据。

It simply adds a new random number to the ListBox, in order to show how the code accesses and modifies the control's data in MainWindow.

这篇关于如何从WPF中的另一个类操作窗口对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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