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

  • SEARCH
  • Home
  • 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 8220189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:26:24+00:00 2026-06-07T13:26:24+00:00

I know that in XAML it’s possible to create data templates with code so

  • 0

I know that in XAML it’s possible to create data templates with code so you can style & bind controls to your liking:

    <ListBox x:Name="statusBox">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid x:Name="ListBoxItemLayout" >
                    <StackPanel>
                        <TextBlock x:Name="time" Style="{StaticResource PhoneTextNormalStyle}" Margin="0" Width="462" Text="{Binding time}" FontSize="16" FontWeight="Bold"/>
                        <TextBlock x:Name="status"  Style="{StaticResource PhoneTextNormalStyle}" Margin="0" Width="462" Text="{Binding status}" TextWrapping = "Wrap" Height="85" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

 public class status
    {
        public string time{ get; set; }
        public string statusText{ get; set; }
    }


        List<status> list = new List<status>();

        status aStatus = new status() { time="3:00pm", statusText="this is a status" };
        list.Add(aStatus);

        statusBox.ItemsSource = list;

However, in my latest project I have a pivot control where items/pages are added dynamically, so I can’t define any xaml on the page. Is there any workaround to this?

What I want to be able to do is create a data template through c# code only, so then I could instantiate a new control in my application.

                List<status> list = new List<status>();
                statusBox lb = new statusBox(); // <-------------------- look here

                status aStatus = new status() { time="3:00pm", statusText="this is a status" };
                list.Add(aStatus);
                list.Add(aStatus);

                lb.ItemsSource = list;

                PivotItem pi = new PivotItem();
                pi.Content = lb;
                Pivot pivot = pivot1;
                pivot.Items.Add(pi);

Is it possible to create a custom control in this way? if so, how?

  • 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-06-07T13:26:25+00:00Added an answer on June 7, 2026 at 1:26 pm

    First, create one DataTemplate in the phone resources section

    <phone:PhoneApplicationPage.Resources>
        <DataTemplate x:Name="listBoxTemplate">
            <Grid >
                <StackPanel>
                    <TextBlock Style="{StaticResource PhoneTextNormalStyle}" Margin="0" Width="462" Text="{Binding time}" FontSize="16" FontWeight="Bold"/>
                    <TextBlock Style="{StaticResource PhoneTextNormalStyle}" Margin="0" Width="462" Text="{Binding statusText}" TextWrapping = "Wrap" Height="85" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </phone:PhoneApplicationPage.Resources>
    

    and then use the following code in the codebehind file to generate the ListBox dynamically

    ListBox lb = new ListBox() { Name = "statusBox" };
    lb.ItemTemplate = this.listBoxTemplate;
    
    List<status> list = new List<status>();
    status aStatus = new status() { time = "3:00pm", statusText = "this is a status" };
    list.Add(aStatus);
    list.Add(new status() { time = "4:00pm", statusText = "this is another status" });
    
    lb.ItemsSource = list;
    this.ContentPanel.Children.Add(lb);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that it is possible to parse an XAML - file at runtime
I know that in XAML you can change a ToolTip's binding with... <TextBlock Text=ABC
I know that in XAML/WPF, I can set the Window size using the Width
I know that we can't use messageBox in xaml. But..Can we have a custome
I know that this line of code will make the cell text-wrap: $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setWrapText(true); 'D1'
I know that I can hijack a form by showing a login form in
I know that many threads has been created here & on the internet about
I know that i can hide anything in codebehind, in selectionchanged event handler. But
I know that I can make a setter that checks to see if a
I know that WPF 3.5 SP1 supports a StringFormat in a binding, but can

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.