开发者

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

开发者 https://www.devze.com 2022-12-13 02:31 出处:网络
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

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 wa开发者_StackOverflow社区s something like: form.Controls;


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


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消