Programmatically create Pivot item and add stack panel dynamically?

c#, pivot, windows, windows-phone-7

Solution

As PivotItem derives from ContentControl you should assign content property of the PivotItem. More info here http://msdn.microsoft.com/en-US/library/windowsphone/develop/microsoft.phone.controls.pivotitem%28v=vs.105%29.aspx

void ws_getMenuCompleted(object sender, getMenuCompletedEventArgs e)
        {
            PivotItem pvt;
            for (int i = 0; i < e.Result.menu.Length; i++)
            {
                pvt = new PivotItem();
                pvt.Header = e.Result.menu[i].name.ToLower();
                var stack = new StackPanel();
                pvt.Content = stack;
                pvtRestaurante.Items.Add(pvt);
                pvt = null;
            }
}

Problem

I have a `Pivot` control in my Windows Phone 7 application, and would like to dynamically add `PivotItem`s that contain additional controls laid out in a `StackPanel`. How can I do that programmtically? I tried to add to PivotItem's `children`, but children doesn't exist in my PivotItem. ``` void ws_getMenuCompleted(object sender, getMenuCompletedEventArgs e) { PivotItem pvt; for (int i = 0; i < e.Result.menu.Length; i++) { pvt = new PivotItem(); pvt.Header = e.Result.menu[i].name.ToLower(); StackPanel panel = new StackPanel(); // ... UI creation in StackPanel removed... pvt.Children = panel; // << This doesn't work. pvtRestaurante.Items.Add(pvt); pvt = null; } } ```

Original source