简体   繁体   中英

WPF C# Change button content from child window

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??

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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