C# 通过c代码创建数据绑定控件?

C# 通过c代码创建数据绑定控件?,c#,windows-phone-7,xaml,itemssource,databound,C#,Windows Phone 7,Xaml,Itemssource,Databound,我知道,在XAML中,可以使用代码创建数据模板,这样您就可以根据自己的喜好对控件进行样式设置和绑定: <ListBox x:Name="statusBox"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <StackPanel /> </ItemsPanelTemplate> &

我知道,在XAML中,可以使用代码创建数据模板,这样您就可以根据自己的喜好对控件进行样式设置和绑定:

    <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;

公共阶级地位
{
公共字符串时间{get;set;}
公共字符串statusText{get;set;}
}
列表=新列表();
status aStatus=newstatus(){time=“3:00pm”,statusText=“这是一个状态”};
列表。添加(aStatus);
statusBox.ItemsSource=列表;
但是,在我最新的项目中,我有一个pivot控件,其中动态添加了项目/页面,因此我无法在页面上定义任何xaml。有什么解决办法吗

我想做的是只通过c代码创建一个数据模板,这样我就可以在我的应用程序中实例化一个新控件

                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);
List List=新列表();

statusBox lb=新的statusBox();// 首先,在电话资源部分创建一个数据模板

<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>

然后在codebehind文件中使用以下代码动态生成ListBox

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);
ListBox lb=new ListBox(){Name=“statusBox”};
lb.ItemTemplate=this.listBoxTemplate;
列表=新列表();
status aStatus=newstatus(){time=“3:00pm”,statusText=“这是一个状态”};
列表。添加(aStatus);
添加(新状态(){time=“4:00pm”,statusText=“这是另一个状态”});
lb.ItemsSource=列表;
this.ContentPanel.Children.Add(lb);

谢谢,我没有看到ItemTemplate属性!:)