1

I want to have a page that has a Table View with just Entry Cells. It's an "Update Details" feature inside the app. I want to make it more user friendly as in I separate the entry cells into two parts and fill the top ones with the data that's already in the object, and what's null I put a placeholder instead.

So far I just have ifs and elses everywhere. I check every single attribute and see if it's null. If not, I put its value inside the entry cell. If it is, I add a placeholder instead.

            if (car.Make != null)
                Weknow.Add(new EntryCell {Label = "Make", Text = car.Make});
            else
                Wedont.Add(new EntryCell {Label = "Make", Placeholder = "Eg. Ford"});

            if (car.Manufacturer != null)
                Weknow.Add(new EntryCell {Label = "Manufacturer", Text = car.Manufacturer});
            else
                Wedont.Add(new EntryCell {Label = "Manufacturer", Placeholder = "Eg. Ford Werke AG"});

            if (car.Plant != null)
                Weknow.Add(new EntryCell {Label = "Plant", Text = car.Plant});
            else
                Wedont.Add(new EntryCell {Label = "Plant", Placeholder = "Eg. Koeln-Niehl"});

            if (car.ModelYear != null)
                Weknow.Add(new EntryCell {Label = "ModelYear", Text = car.ModelYear});
            else
                Wedont.Add(new EntryCell {Label = "ModelYear", Placeholder = "Eg. 2010"});

            if (car.SequentialNumber != null)
                Weknow.Add(new EntryCell {Label = "SequentialNumber", Text = car.SequentialNumber});
            else
                Wedont.Add(new EntryCell {Label = "SequentialNumber", Placeholder = ""});

            if (car.Model != null)
                Weknow.Add(new EntryCell {Label = "Model", Text = car.Model});
            else
                Wedont.Add(new EntryCell {Label = "Model", Placeholder = "Eg. Focus"});

Weknow is the table section containing the Entry cells that have values, and Wedont is the section with the ones that are null, and have a placeholder.

The end result is this: https://i.stack.imgur.com/EbTVB.jpg

Everything works, but I'm wondering if there is any simpler way to go through an object and see if properties are null and if they are, then there should be a placeholder instead. I was thinking of having a Binding Context but I wanted to do all of this in code rather than XAML. But if it can be done that way, I could try.

Thanks!

  • https://stackoverflow.com/questions/957783/loop-through-an-objects-properties-in-c-sharp – Jason May 31 '19 at 17:19
  • In most other Xamarin controls, you'd always have placeholder set, and then it would be replaced by the Text property if there was one set. Have you tried doing something like `Weknow.Add(new EntryCell {Label = "Model", Placeholder = "Eg. Focus", Text = car.Model});` without the if statement and seeing if the control handles it for you? See info on the [Xamarin.Forms Placeholder Property](https://learn.microsoft.com/en-us/dotnet/api/xamarin.forms.entrycell.placeholder?view=xamarin-forms#Xamarin_Forms_EntryCell_Placeholder) for more information. – Andrew May 31 '19 at 17:23
  • The problem with that is that I want to add the not null values in the Weknow part. If I do as you said, I guess it would work, but it would put everything in the same category. – Dorian Olarescu May 31 '19 at 17:26
  • create all the EntryCells and place them in a List. Then iterate through them and place ones with Text in one section and ones with empty Text in another – Jason May 31 '19 at 18:26

1 Answers1

1

The way the Placeholder Property works in Xamarin Forms is to only display if the Text Property is null or empty. If Text is not null or empty, then Text will be displayed and Placeholder will not. Given this you could update your calls to something like:

Weknow.Add(new EntryCell {Label = "Make", Placeholder = "Eg. Ford", Text = car.Make});
Weknow.Add(new EntryCell {Label = "Manufacturer", Placeholder = "Eg. Ford Werke AG", Text = car.Manufacturer});
Weknow.Add(new EntryCell {Label = "Plant", Placeholder = "Eg. Koeln-Niehl", Text = car.Plant});

// Continue with other cells.

This allows you to drop the conditional statements.

Andrew
  • 1,390
  • 10
  • 21