Create ToolbarItems, Binding Context and layout dynamically Xamarin forms

SiddySiddy Member ✭✭

I have a tableview which gets generated dynamically. I want to add toolbar items on Mainpage and a label at top, but don't know how to add that to my main Content page, which already takes in tableview object.

Also with that I want to add INotify property change, to bind the UI elements, and that too dynamically.

Following is my sample code:

TableView mainTable = new TableView();
TableRoot root = new TableRoot();

    foreach (var sections in titles)
    {
        var layout = new StackLayout();
        var labels = from s in models
                     where s.Title == sections
                     select s;                

        foreach (var label in labels)
        {
            var forgetPasswordLabel = new Label   // Your Forget Password Label
            {
                Text = label.Lists,
                FontSize = 20,
                TextColor = Color.Blue,
                HorizontalOptions = LayoutOptions.Center,
            };

            var forgetPassword_tap = new TapGestureRecognizer();
            forgetPassword_tap.Tapped += (s, e) =>
            {                        
                DisplayAlert("Tapped", "This is a tapped Span. " + label.Lists, "OK");
            };
            forgetPasswordLabel.GestureRecognizers.Add(forgetPassword_tap);                                        

            layout.Children.Add(forgetPasswordLabel);                                                       
        }
        root.Add(new TableSection(sections)
                {
                    new ViewCell() {View = layout}
                });
    }
    mainTable.Root = root;            
    Content = mainTable;

With the above code I want to dynamically add toolbaritems also on the Page, a Label at the top and notification property event for individual properties. I am getting stuck on how to assign all these elements to my main Content page.

Any help or code suggestions would be great.

Thanks In Advance!!!..

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    For ToolbarItem, you can try this, for example:

    var settings = new ToolbarItem
            {
                Icon = "img123.png",
                Name = "Settings",
            };
    
    this.ToolbarItems.Add(settings);
    
  • jezhjezh Member, Xamarin Team Xamurai

    @Siddy
    Have you resolved this question?

Sign In or Register to comment.