Home > OS >  How to avoid the overwriting in the DataBinding?
How to avoid the overwriting in the DataBinding?

Time:12-20

im new at programming and need help here... I want to create a Binding with a Combobox Item. But the DataBinding is not adding a new DataBind, it overwrites the old one because of the loop. So i want if you select a "Profilname" in the Combobox that the "Path" will be displayed.

But so far, just the last loaded .txt file will be displayed because of the overwrite.

Here is now my question: How to avoid the overwrite of the DataBind in the (foreach)-loop?

For information: There is a folder which contains many .txt-files, which are all called: "profile.txt". The Programm search for all the files with a loop and then search in the files with another loop a line, which contains the word "profile_name". And then the Name has to be displayed in the ComboBox and the Path has to be binded to the "Item"/"Text" in the ComboBox.

I hope this is understandable and sorry if my code is confusing or not very strong written, im learning...

            foreach (string profiletxt in Directory.EnumerateFiles(profiledirectory, "profile.txt", SearchOption.AllDirectories))
            {
                foreach (string line in System.IO.File.ReadAllLines(profiletxt))
                {
                    if (line.Contains("profile_name"))
                    {
                        string remLine = line.Remove(0, 15);
                        string dLine = remLine.Replace("\"", "");
                        // dataBinding
                        var listProfiles = new List<Profile>() {
                        new Profile() {Path = profiletxt, Name = dLine,},
                        };

                        materialComboBox1.DataSource = listProfiles;
                        materialComboBox1.DisplayMember = "Name";
                        materialComboBox1.ValueMember = "Path";
                    }
                    
                }
                if (materialComboBox1.SelectedIndex == -1)
                {
                    MessageBox.Show("Error, couldn't find Profiles");
                }
            }


        public class Profile
        {
            public string Path {   get; set; }
            public string Name { get; set; }
        }

CodePudding user response:

a ComboBox uses its ItemSource containing the available items. In your inner foreach loop you declare a new profile list for every find of profile item:

var listProfiles = new List<Profile>() {
    new Profile() {Path = profiletxt, Name = dLine,},
};

materialComboBox1.DataSource = listProfiles;

Instead, you'd probably like to create a new Profile list before the first foreach loop

var listProfiles = new List<Profile>();

and in the inner loop, add your new finding to the list

listProfiles.Add(new Profile() {Path = profiletxt, Name = dLine});

Then, after the outer loop, you may assign the new ItemSource only once.

There are other newby design flaws in your code:

  1. there should be no need to set DisplayMember and ValueMember in the .xaml.cs "code behind". Rather it belongs into the xaml code itself as these are static.

  2. As a more general advise, consider not doing any kind of "business rules stuff" or data holding in your code behind. Rather you like to separate your UI ("View") from your data ("Model") while a "ViewModel" separates these two and implements the business rules. There are tons of good introductions on this MVVM programming pattern out there.

  • Related