0

My problem is this: I'm trying to code an c# wpf application in in which the user can select an item from a combobox in the main window. I then want to pass that value on to a user control, which is then supposed to print data referring to the selected comboBoxItem onto the screen. How can I achieve this? Regards Curtis

Curtis
  • 65
  • 1
  • 1
  • 8
  • show us your effort, your code, what have you tried? – Yuliam Chandra Jul 11 '14 at 09:14
  • you can simply create property in UserControl and before opening in control just assign value to that particular property and at usercontrol level you will have that property in hand – Ashok Rathod Jul 11 '14 at 09:31
  • OK, just before checking stackexchange again, I found a workaround. Although I don't think it's very elegant. I created an instance of my user control in my main window code and then passed 'object sender' to a function called 'updateList(object sender)' in my user control. In my updateList method in the user control I can then use the selected item. – Curtis Jul 11 '14 at 10:31

1 Answers1

0

There are several ways for you to pass a value from the MainWindow to a child Window or UserControl. One of the simplest ways is for you to declare a DependencyProperty in your UserControl and then data bind to that property in the MainWindow in XAML:

<ComboBox Name="Combo" ... />
<YourPrefix:YourUserControl YourProperty="{Binding SelectedItem, ElementName=Combo}" />

This will update the property value in your UserControl each time the user selects another item.

Please see my answer to the How to call functions in a main view model from other view models? question for a more complex and flexible way to pass values.

Community
  • 1
  • 1
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Thank you. I'll try that. I haven't done anything with data binding yet and since I've stumbled across it so many times, it's definitely worth having a look at. – Curtis Jul 11 '14 at 10:34