枢轴控制 - 数据绑定问题

时间:2011-04-16 05:30:23

标签: c# windows-phone-7 pivot

我已经检查了各种Pivots的例子,并认为我的实现应该有效但是它有问题。

这是XAML:

        <controls:Pivot  Title="Results"  ItemsSource="{Binding baskets}"> 
        <controls:Pivot.HeaderTemplate> 
            <DataTemplate> 
                <Grid x:Name="grid"> 
                    <TextBlock Text="{Binding basketName}" /> 
                </Grid> 
            </DataTemplate> 
        </controls:Pivot.HeaderTemplate> 
        <controls:Pivot.ItemTemplate> 
            <DataTemplate> 
                <Grid> 
                    <StackPanel Orientation="Vertical"> 
                        <TextBlock TextWrapping="Wrap" Text="{Binding basketItems.Count}"/> 
                    </StackPanel> 
                </Grid> 
            </DataTemplate> 
        </controls:Pivot.ItemTemplate> 
    </controls:Pivot> 

和代码隐藏:

        public ObservableCollection<Basket> baskets = new ObservableCollection<Basket>(); 



    public pivotPage() 
    { 
        InitializeComponent(); 


        //for testing purposes 
        baskets.Add(new Basket()); 
        baskets.Add(new Basket()); 

    } 

页面呈现空白,我做错了什么?

1 个答案:

答案 0 :(得分:1)

您的代码中没有迹象表明您已设置DataContext。请尝试以下方法:

public ObservableCollection<Basket> baskets = new ObservableCollection<Basket>(); 

public pivotPage() 
{ 
    InitializeComponent(); 

    this.DataContext = baskets;

    //for testing purposes 
    baskets.Add(new Basket()); 
    baskets.Add(new Basket()); 

}