Reputation: 321
I apologize if the title is bad, I do not exactly know how to express myself about this issue.
I have a program written in C#, WPF, where I have a number of TabItems. inside these TabItems I have a couple of textboxes, checkboxes etc. Inside these "main" TabItems are also additional TabItems containing textboxes, checkboxes etc. What I want to do is to be able to store all values from these controls in a xml file. I have managed to do so for all the visible ones by standing in one of the main TabItems and executing the following code:
List<TextBox> allTxt = FindVisualChildren<TextBox>(this).ToList<TextBox>(); // this here is my MainWindow (Interaction logic for MainWindow.xaml)
foreach (TextBox t in allTxt)
list.Add(t.Name + ":" + t.Text); // list is a List I store these in
//The same goes later for checkboxes etc.
I then store these in the desired file and this works fine, for the visible ones. But how can I do in order to include all the controls from all the "second" TabItems as well as all the ones from the "main" ones? I have tested to change the Selected TabItem to increse this one after the first one is complete but this does not seem to be working...
Upvotes: 1
Views: 1615
Reputation: 56697
As an alternative to iterating all the controls you could also consider data-binding them. That way you'd just have to get the data from the data source and write it to the XML file.
There'd have to be a property for each control in the class and in your XAML you'd have to bind each control to its property. Then you'd have to set the window's data context to an instance of the class.
You might want to read a bit about WPF data binding.
Upvotes: 0
Reputation: 11025
Perhaps something of a "brute force" method, but it's clean and easy to read...
List<String> textBoxes = new List<String>();
List<String> checkBoxes = new List<String>();
foreach (TabPage mainPage in mainTabControl.TabPages)
{
foreach (Control c in mainPage.Controls)
{
if (c is TabControl)
{
foreach (TabPage secondPage in ((TabControl)c).TabPages)
{
foreach (Control c2 in secondPage.Controls)
{
if (c is CheckBox)
checkBoxes.Add(((CheckBox)c).Name + ":" + (((CheckBox)c).Checked ? "True" : "False"));
else if (c is TextBox)
textBoxes.Add(((TextBox)c).Name + ":" + (((TextBox)c).Text));
//... add more for other controls to capture
}
}
}
else
{
if (c is CheckBox)
checkBoxes.Add(((CheckBox)c).Name + ":" + (((CheckBox)c).Checked ? "True" : "False"));
else if (c is TextBox)
textBoxes.Add(((TextBox)c).Name + ":" + (((TextBox)c).Text));
//... add more for other controls to capture
}
}
}
Upvotes: 1
Reputation: 1
You should give names to inner tabs and then search tham using their names. Here you can find some explanation: find control by name
Upvotes: 0