0

I am trying to create a simple onscreen keypad created using buttons (currently a User-control), on those buttons i have a click event, when i click/touch a button i want the value of that button sent to a Text-block in my Main-window.

I can't/don't understand how to make the User-control (keypad) see the Text-block (in Main-window) to add in the value that i need.

I have seen solutions that use command Bindings and solutions that use the visual tree traversing but all of them are the main window accessing the user control, not the other way around.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • 1
    Well, one approach would be to expose a property in the control, and bind your textblock in the MainWindow to that property. Change the value of the property at button click. There might be better solutions though. – Rachit Kyte.One Mar 19 '14 at 11:16

2 Answers2

0

All the examples are the other way around because that is how a UserControl is supposed to work.

A UserControl is a packaged piece of re-usable functionality. It should not know anything about the code that is using it.

Instead you should expose routed events in your UserControl for things like a when number was selected, and subscribe to them in your main window.

GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103
0

There are many ways to achieve what you want. If your MainWindow.xaml has a UserControl and you want to react to a change from the control in the MainWindow.xaml.cs file, then you could add a delegate to the UserControl code behind and register a handler for it in the MainWindow.xaml.cs file. Implementing new delegates are generally somewhat simpler than implementing RoutedEvents, which is another way that you could handle this situation.

Using a delegate like this will enable you to effectively pass a signal to the main view from the child UserControl code behind, which you can react to in any way you want to. Rather than explain the whole story again here, please see my answers from the Passing parameters between viewmodels and How to call functions in a main view model from other view models? posts here on Stack Overflow for full details on how to achieve this.

Community
  • 1
  • 1
Sheridan
  • 68,826
  • 24
  • 143
  • 183