Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Text property not set until focus is lost

$
0
0

I have a ContentPage (see below) with two EntryCell's on it and a "Done" toolbar item.

The page is for maintaining a "Category" SQLite table (see below).

When adding a new category the "Text" property of the "Description" is blank until focus is lost. So upon creating a new Category and entering something for "Name" and then "Description", and then clicking the "Done" toolbar item, the "Text" property of "Description" is blank.

I've narrowed it down to having to move the focus off of "Description" before the "Text" property is set on the "Description"'s EntryCell.

Here's the code. Any thoughts?

    public class Category
    {
        [PrimaryKey, AutoIncrement]
        public int ID { get; set; }

        public string Name { get; set; }
        public string Description { get; set; }

        public Category()
        {
            Name = string.Empty;
            Description = string.Empty;
        }
    }


    public class CategoryMaintPage : ContentPage
    {
        public CategoryMaintPage()
        {
            var header = new Label
            {
                Text = "Category Maintenance",
                HorizontalOptions = LayoutOptions.Center
            };

            var tcName = new EntryCell {Label = "Name"};
            tcName.SetBinding(EntryCell.TextProperty, "Name");

            var tcDescription = new EntryCell {Label = "Description"};
            tcDescription.SetBinding(EntryCell.TextProperty, "Description");

            var tableView = new TableView
            {
                Intent = TableIntent.Form,
                Root = new TableRoot
                {
                    new TableSection
                    {
                        tcName,
                        tcDescription
                    }
                }
            };

            Content = new StackLayout
            {
                Children = {header, tableView}
            };

            var done = new ToolbarItem
            {
                Name = "Done",
                Command = new Command(async () =>
                {
                    var category = (Category) BindingContext;
                    if (string.IsNullOrEmpty(category.Name) ||
                        string.IsNullOrEmpty(category.Description))
                    {
                        await DisplayAlert("Entry Error", "Both Name and Description are required", "Ok");
                        return;
                    }
                    App.Database.SaveCategory(category);
                    await Navigation.PopAsync();
                })
            };
            ToolbarItems.Add(done);
        }
    }

Viewing all articles
Browse latest Browse all 58056

Trending Articles