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?

有帮助吗?

解决方案

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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top