[英]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.