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
2009-11-26 16:05:59
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
2009-11-26 16:12:03
+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
2009-11-26 16:09:30
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
2009-11-26 16:24:33
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
2009-11-26 16:30:25
+1 - looks like I missed the point of the question, so may as well upvote the right one.
MoominTroll
2009-11-26 16:39:02
@Vytas : I noticed that you have never voted. You shoud read the faq I think. Cheers, peter
Peter
2009-11-26 16:43:13