Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4045046
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:22:10+00:00 2026-05-20T13:22:10+00:00

can someone give the C# equivalent of this xamle code? <ListBox Width=400 Margin=10 ItemsSource={Binding

  • 0

can someone give the C# equivalent of this xamle code?

<ListBox Width="400" Margin="10"
     ItemsSource="{Binding Source={StaticResource myTodoList}}">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <StackPanel>
        <TextBlock Text="{Binding Path=TaskName}" />
        <TextBlock Text="{Binding Path=Description}"/>
        <TextBlock Text="{Binding Path=Priority}"/>
      </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

That’s because I would like to know how to dynamically add PivotItems into a Pivot, for a windows phone 7 app, with the default Pivot layout generate in VS 2010. For instance, I got this code, but I didn’t find a way to add the StackPanel object sp, to ListBox:

 // epgXml is an XElement object, wich has channel and programmes tags
 // it's an app about a tv guide, where the user clicks on a channel and gets a list of programmes for that channe.
 // in th xaml code, I have a grid with a pivot object in it:
  <Grid x:Name="LayoutRoot" Background="Transparent">
    <controls:Pivot x:Name="MainPivot" ItemsSource="{Binding PivotItems}" Title="ZON EPG">

    </controls:Pivot>
</Grid>

// In MainViewModel.cs I have this code:
public void client_GetEPGCompleted(Object sender, GetEPGCompletedEventArgs e)
{
        DataTemplate dt = new DataTemplate();
        epgXml = e.Result.Output.EPGXml;
        if (epgXml != null)
        {
            //Channels = new List<string>();
            // parse the xml and show channels and programmes
            var channels = from c in epgXml.Elements("channel")
                           select c;
            foreach (XElement el in channels)
            {
                PivotItem pi = new PivotItem();
                pi.Header = el.Elements("display-name").FirstOrDefault().Value;

                ListBox lb = new ListBox();
                lb.Margin = new Thickness(0, 0, -12, 0);

                //ItemsControl ic = new ItemsControl();
                //ic.ItemTemplate = dt;

                lb.ItemTemplate = dt;
                StackPanel sp = new StackPanel();
                sp.Margin = new Thickness(0, 0, 0, 17);
                sp.Width = 432;

                TextBlock tb = new TextBlock();

                Binding binding = new Binding("ProgTitle");
                binding.Mode = BindingMode.TwoWay;
                tb.SetBinding(TextBlock.TextProperty, binding);

                tb.Margin = new Thickness(12, 0, 0, 0);
                tb.TextWrapping = TextWrapping.NoWrap;
                tb.Style = Application.Current.Resources["PhoneTextExtraLargeStyle"] as Style;
                sp.Children.Add(tb);

                tb = new TextBlock();
                binding = new Binding("ProgStart");
                binding.Mode = BindingMode.TwoWay;
                tb.SetBinding(TextBlock.TextProperty, binding);

                tb.Margin = new Thickness(12, -6, 0, 0);
                tb.TextWrapping = TextWrapping.NoWrap;
                tb.Style = Application.Current.Resources["PhoneTextSubtleStyle"] as Style;
                sp.Children.Add(tb);

                tb = new TextBlock();
                binding = new Binding("Duration");
                binding.Mode = BindingMode.TwoWay;
                tb.SetBinding(TextBlock.TextProperty, binding);

                tb.Margin = new Thickness(12, 0, 0, 0);
                tb.TextWrapping = TextWrapping.NoWrap;
                tb.Style = Application.Current.Resources["PhoneTextSubtleStyle"] as Style;
                sp.Children.Add(tb);

                // get programmes foreach channel
                var progrs = from p in epgXml.Elements("programme")
                             where p.Attributes("channel").FirstOrDefault().Value == el.Attributes("id").FirstOrDefault().Value
                             select new ItemViewModel()
                             {
                                 ProgTitle = p.Elements("title").FirstOrDefault().Value,
                                 ProgStart = p.Attributes("start").FirstOrDefault().Value,
                                 Duration = p.Elements("length").FirstOrDefault().Value
                             };
                foreach (ItemViewModel item in progrs)
                {
                    this.Items.Add(item);
                }
                // create new instance - this holds all the programms for each channel
                this.Items = new ObservableCollection<ItemViewModel>();
                PivotItems.Add(pi);
}

Thx in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T13:22:10+00:00Added an answer on May 20, 2026 at 1:22 pm

    You have to use XamlReader to create datatemplates (as in your XAML) on the phone.

    See a full example and explanation at http://www.windowsphonegeek.com/tips/wp7-dynamically-generating-datatemplate-in-code

    For an example off dynamically adding PivotItems seee How to dynamically add pivot item and add Image to each pivot item?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone give me some example code that creates a surface with a transparent
Title is the entire question. Can someone give me a reason why this happens?
I'm sorry, but this keyword doesn't mean anything to me... Can someone give me
This really, really urks me, so I hope that someone can give me a
Can someone please give the Java equivalent of the below python (which slices a
This seems horrible inefficient. Can someone give me a better Ruby way. def round_value
Can someone give an example of a good time to actually use unsafe and
Can someone give some hints of how to delete the last n lines from
Guys, can someone give me a brief run through of how to change the
When is it appropriate to use CoTaskMemAlloc? Can someone give an example?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.