【问题标题】:Creating a databound control through c# code?通过 C# 代码创建数据绑定控件?
【发布时间】:2012-07-11 20:54:19
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案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>
    

    然后在代码隐藏文件中使用以下代码动态生成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);
    

    【讨论】:

    • 谢谢,我没有看到 ItemTemplate 属性! :)
    猜你喜欢
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 2018-06-26
    • 2010-12-01
    相关资源
    最近更新 更多