Vytas
Vytas

Reputation: 1291

WPF - It is possible to get all UI controls with their values

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;

Upvotes: 1

Views: 1689

Answers (2)

Peter
Peter

Reputation: 48998

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);
    //[...]
}

Upvotes: 2

MoominTroll
MoominTroll

Reputation: 2552

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;

Upvotes: 0

Related Questions