WPF C# Change button content from child window

久未见 提交于 2020-01-06 08:23:06

问题


I'm developing a touchscreen app. I have a form with buttons with the content "new player" and then a keyboard with a textbox is shown for the user to enter his name. Now I need that when I close (or while inputting the name) the button from the parent window change the content to the name that the user typed. But I cant make a binding to the parent.. how can I do it??


回答1:


Are you implementing the Model-View-ViewModel pattern in your WPF application, or using databinding at all?

You don't have to use MVVM, but you should really be using databinding in some fashion.

With databinding, you'd bind the button content to a string property of some object (a view-model in MVVM). That object needs to implement the INotifyPropertyChanged interface. You can bind the textbox on the child window to the same property of the same object.

With that in place, the button will be updated automatically when the user types in a new player name.

If you need more details, please comment and I'll be glad to elaborate.




回答2:


You can try something like:

var button = this.Parent.Controls.OfType<Button>().Select(b => b.Name == "NameOfControl").First();
button.Content = name;

Edit: And Jay's idea is the right way at going at this, i was just offering an alternative way of accessing the controls from their parent using LINQ.



来源:https://stackoverflow.com/questions/6654310/wpf-c-sharp-change-button-content-from-child-window

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!