1

I have a StackPanel inside a WPF page, and I am loading the same UserControl inside it depending on the user requests. Now for each of these UserControls, I need to get the items which are inside it. The UserControl consists of a Calendar, and 3 comboboxes. How can I get the items?

So far I have the following:-

foreach (UserControl child in stk.Children)
{

}

and child has the contents required, however I cannot find out how to get the contents of every child to validate and store in the database. I tried child.FindControl("combobox1") but that did not work.

Thanks for your help and time

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
JMon
  • 3,387
  • 16
  • 63
  • 102
  • Where are you calling child.FindControl("combobox1") from? Is you child loaded when you call it? –  Dec 01 '11 at 14:06
  • The child has the data, so do I need to load the user control? Like UCPageControl ucPControl = new UCPageControl? And then move the data in the child to this ucPControl? – JMon Dec 01 '11 at 18:38
  • No you don't have to, but child.FindControl("combobox1") can return null, if you call it before the parent control gets fully loaded. that's why i asked you where do you call it? Are you getting an exception or code just doesn't work? –  Dec 01 '11 at 19:41
  • Hi Dmitry, my child has only the following properties :-FindCommonVisualAncestor, FindName, FindResource, TryFindResource. It does not have FindControl – JMon Dec 02 '11 at 08:19
  • I have found the following thread which was very helpful! http://stackoverflow.com/questions/636383/wpf-ways-to-find-controls.....Thanks for all your help guys – JMon Dec 02 '11 at 08:33

2 Answers2

0

You can get hold of the Content . However I strongly suggest you don't do those of kind of things in the UI, and you shouldn't get the data from the UI. the UI should get the data from a ViewModel or a Model, and update them when needed(This is achieved through Binding). so if you need the data, your ViewModel should be the place to look at, not the UI.

MBen
  • 3,956
  • 21
  • 25
  • Hi Mben I am not using the MVVM pattern yet, doing some basic stuff. So lets say I load the control in the Page, how can I get the data from the Model? – JMon Dec 01 '11 at 18:35
0

I have found the following thread which was very helpful!

How can I find WPF controls by name or type?

It uses a helper to find all the controls in a WPF page.

Thanks for all your help guys

Community
  • 1
  • 1
JMon
  • 3,387
  • 16
  • 63
  • 102