Windows Phone中的ListBox绑定

时间:2014-01-08 11:55:14

标签: windows-phone-8

我想绑定驻留在Pivot Control中的List中的项源。如何在xaml或code中绑定itemsouce。这是我的代码

<controls:Pivot x:Name="Category_pivot"  Foreground="Black" FontSize="22">
                <controls:Pivot.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding}" FontSize="45"></TextBlock>
                    </DataTemplate>
                </controls:Pivot.HeaderTemplate>
                <controls:Pivot.ItemTemplate>
                    <DataTemplate>

            <ListBox x:Name="d" ItemsSource="{Binding Item}">
                <ListBox.ItemTemplate>
                    <DataTemplate>                      
                        <Image x:Name="img" Source="{Binding ImageSource}"   ></Image>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

            </DataTemplate>
                </controls:Pivot.ItemTemplate>
            </controls:Pivot>
       </Grid>

1 个答案:

答案 0 :(得分:1)

我可能花了太多时间在这上面,但它困扰了我......

你在你的例子中使用了Foreground =“Black”,这意味着标题是黑色的,所以在OOB项目中它是黑色的。

这是我的XAML:

    <phone:Pivot x:Name="Category_pivot" FontSize="22">
            <phone:Pivot.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Title}" FontSize="45"></TextBlock>
                </DataTemplate>
            </phone:Pivot.HeaderTemplate>
            <phone:Pivot.ItemTemplate>
                <DataTemplate>
                    <ListBox x:Name="d" ItemsSource="{Binding Items}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding ImageSource}"></TextBlock>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </DataTemplate>
            </phone:Pivot.ItemTemplate>
        </phone:Pivot>

我的C#:

public partial class MainPage : PhoneApplicationPage
{
    public class Data
    {
        public string Title { get; set; }
        public ObservableCollection<SubData> Items { get; set; }
    }

    public class SubData
    {
        public string ImageSource { get; set; }
    }

    // Constructor
    public MainPage()
    {
        InitializeComponent();

        ObservableCollection<Data> list = new ObservableCollection<Data>();
        Data d = new Data() { Title = "my page 1" };
        d.Items = new ObservableCollection<SubData>();
        d.Items.Add(new SubData() { ImageSource = "1" });
        d.Items.Add(new SubData() { ImageSource = "2" });
        list.Add(d);

        d = new Data() { Title = "my page 2" };
        d.Items = new ObservableCollection<SubData>();
        d.Items.Add(new SubData() { ImageSource = "A" });
        d.Items.Add(new SubData() { ImageSource = "B" });
        list.Add(d);

        Category_pivot.ItemsSource = list;
    }
}