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

时间:2012-07-11 20:54:20

标签: c# windows-phone-7 xaml itemssource databound

我知道在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;

但是,在我的最新项目中,我有一个动态添加项目/页面的数据透视控件,因此我无法在页面上定义任何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);

是否可以通过这种方式创建自定义控件?如果是这样,怎么样?

1 个答案:

答案 0 :(得分:1)

首先,在电话资源部分创建一个DataTemplate

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