简体   繁体   English

当我用其他方法更改控件时,为什么不更新控件?

[英]Why don't my controls update when I change them in another method?

public partial class Person : Window
{
    private static TabControl tabControl;
    public Person()
    {
              InitializeComponent();
              tabControl = new TabControl();
              grid.Children.Add(tabControl);
    }

    public void someMethod(String name){
        TabItem newTab = new TabItem();
        newTab.Header = name;
        tabControl.Items.Add(newTab);
    }
}

public class Update{
    public void createGUI(String name){
        Person newPerson = new Person();
        newPerson.someMethod(name);
    }
}

The method someMethod is called in another class at some point after creating the class. 创建类后,有时会在另一个类中调用someMethod方法。 It is called after InitializeComponent and everything in the constructor. 它在InitializeComponent及其构造函数中的所有函数之后调用。 However, the tab control still has no tabs even after someMethod finished. 但是,即使someMethod完成后,选项卡控件仍然没有选项卡。

Edit: To clarify, the Update method is only called once through the lifetime of the application. 编辑:澄清一下,在应用程序的整个生命周期中,Update方法仅被调用一次。 I only want one instance of Person. 我只想要一个Person实例。

public void createGUI(String name){
    Person newPerson = new Person();
    newPerson.someMethod(name);
}

There, you are creating a new Person object which is completely unrelated to the one that is being displayed to you. 在那里,您正在创建一个新的Person对象 ,该对象与正在显示的对象完全无关。 That new object is actually never displayed anywhere, so whatever you change about it, won't be visible to you. 实际上,该新对象永远不会显示在任何地方,因此,无论您对其进行什么更改,您都不会看到。

Instead, you need to change the Person object that is actually around and displayed. 相反,您需要更改实际周围并显示的Person对象。

Alternatively, you could also show the new object by using newPerson.Show() but that will open a new window and not change the existing one. 另外,您也可以使用newPerson.Show()显示新对象,但这将打开一个新窗口,而不更改现有窗口。

原因很简单,您每次更新都创建一个新的Person类实例,因此一个实例中的更改不能反映在另一个实例中。

暂无
暂无

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

相关问题 当我使用Update方法时,我的updatePanel不更新 - My updatePanel don't update when I use the Update method 我在应用程序中更改了一个值,但是没有在数据库中更新 - I change a value in my application , but don't update in my database 当我从另一个进程的标准输出中读取时,为什么我的事件处理程序不触发 - Why don't my event handlers fire when I'm reading from the standard output of another process 当我未在代码中调用它时,为什么我的方法被Unity调用? - Why is my method called by Unity when I don't call it in code? 当我实际上未在代码中进行更改时,为什么椭圆的不透明度会自动更改? - Why is the opacity of my ellipse changing automatically when I don't actually change it in the code? 当我将我的程序放入不同的方法中,并将它们组合在一个循环中时,它们不能正确地相互读取。 有谁知道为什么? - When I put my program into different methods, and combine them in a loop, they don't read each other properly. Anyone know why? 为什么我在Visual Studio中的对象浏览器中找不到System.Windows.Controls命名空间? - Why don't I find the System.Windows.Controls namespace in my object browser in Visual Studio? 在将方法分配给委托时,为什么不添加括号? - Why don't I add parantheses when assigning a method to a delegate? 为什么我的XDocument在我不想要时保存声明? - Why is my XDocument saving the declaration when I don't want it to? 当我从另一种形式调用方法时,控件不会更改颜色或文本 - Controls do not change color or text, when I call method from another form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM