views:

71

answers:

2

It is possible in WPF to get all UI controls with their values? In example i have some window with some textboxes and in another window I want to get entered values from the first window textboxes or other input elements. In WinForms it was something like: form.Controls;

A: 

Can't you just name the textboxes in the first window and pull the text value out?

<!--textbox in window 1-->
<TextBox Name="myFirstTextBox>Hello</TextBox>

 <!--textbox in window 2-->
 <TextBox Name="mySecondTextBox></TextBox>


  //in your code behind, when second window opens
  mySecondTextBox.Text = myFirstTextBox.text;
MoominTroll
Actually i need this "trick" to do job like this - get some data from database and automaticaly put values from DataRow to UI controls, such as TextBoxes, Comboboxes and so on. It should be dony by element name. In example if TextBox Name is "AbilityID", this textbox must be filled with data from this datarow column "AbilityID". Yuor example is not universal, its like a hardcode.
Vytas
+1  A: 

Of course you have to know what kind of property you want, bur you could check, although you should probably have some OO pattern taking care of the behaviour instead of iffing every control

 public string GetValue(Control x)
 {
  if (x is TextBox) return ((TextBox) x).Text;
  if (x is ComboBox) return ((ComboBox)x).SelectedValue.ToString();
  if (x is Label) return ((Label)x).Content .ToString();
  //...
 }


 foreach (Control x in theGrid.Children)
 {
       string field = GetValue(x);
  //[...]
 }
Peter
I putted canvas in my window and some textboxes in that canvas, but theres something wrong wiht yuor code: var children = (from c in canvas1.Children select c);Error: canot resolve symbol "select".
Vytas
I have changed it now in an UIEleement collection
Peter
Yuor edited code seems looks like the thing that I was looking for. But how to check if x is sure TextBox? In grid may be a lot of different elements, such as labels, comboboxes and so on.
Vytas
This is quick and dirty, but you can check just check of course
Peter
Ok, good answer. Thank you.
Vytas
+1 - looks like I missed the point of the question, so may as well upvote the right one.
MoominTroll
haha, tx :-) than at least so is
Peter
@Vytas : I noticed that you have never voted. You shoud read the faq I think. Cheers, peter
Peter