Dynamic List with foreach

Devellopy

New member
Joined
Jul 25, 2021
Messages
4
Programming Experience
Beginner
Hi :)

I work at the moment on a database wish refer several customers. I've create a page with textboxes, each with specific x:name (FirstnameFrame, LastnameFrame, BirthdateFrame,...).

I've a first class for customer :

C#:
[SerializableAttribute]
public class Customer
{
    public string Frame { get; set; }
    public string Value { get; set; }
}

And a second for create the list :

C#:
public partial class CreatingFile : Window
    {
        public List<Customer> customerData = new List<Customer>();
        public string info;
        public string filePath => $@"C:\Users\computer\Sofwares\Visual Studio\UserCodes\Customer file\Customer file\data\{info}.save";
        public IEnumerable<Control> Controls { get; private set; }

        public CreatingFile()

        {
            InitializeComponent();
        }
        
        private void SaveBtnClick(object sender, RoutedEventArgs e)
        {
            Customer data = new Customer();

            foreach (Control item in Controls)
            {
                if (item is TextBox)
                {
                    data.Frame = /*Missing value*/;
                    data.Value = /*Missing value*/;
                    
                    customerData.Add(data);
                }
            }
            
            info = $"{NameFrame.Text}_{LastnameFrame.Text}_{PasswordFrame.Text}";
            
            Serialise.Save(filePath, customer);

            Application.Current.Shutdown();
        }

I have a problem at the lines 21 and 22. I would like to create a list like that :

C#:
        |        Frame        |         Value       |
Index 0 | FirstnameFrame.Name | FirstnameFrame.Text |
Index 1 | LastnameFrame.Name  | LastnameFrame.Text  |
Index 2 | BirthdateFrame.Name | BirthdateFrame.Text |
Index 3 |...

So is there a way to assign Firstname, Lastname, Birthdate,... in a variable "i" at each loop ?

To have a code like that :

C#:
private void SaveBtnClick(object sender, RoutedEventArgs e)
        {
            Customer data = new Customer();
            string i;

            foreach (Control item in Controls)
            {
                if (item is TextBox)
                {
                    data.Frame = {i}Frame.Name;
                    data.Value = {i}Frame.Text;
                    
                    customerData.Add(data);
                }
            }
            
            info = $"{NameFrame.Text}_{LastnameFrame.Text}_{PasswordFrame.Text}";
            
            Serialise.Save(filePath, customer);

            Application.Current.Shutdown();
        }

Thank you for your help.
 
Part of the problem here is that it looks like you are trying to use WPF as if it were a WinForms where you are trying to access controls by ID and name. Don't do that. If you are going to use WPF, use WPF the way it was intended -- using the MVVM pattern. With the MVVM pattern, you use data binding and let the data live in collections and the UI simply reflects the state of the data model. You don't have to explicitly move data to/from the UI.
 
Thank you Skydiver, but i'm looking for since a moment and i don't see how data binding can help me to automatically create a list of textboxes.
 
With WPF's item and data templates you don't even have to create a list of textboxes.
 
Last edited:
Back
Top Bottom