在Pivot.HeaderTemplate中绑定文本

时间:2011-02-13 21:25:32

标签: c# xaml data-binding windows-phone-7

我试图为自己解决问题几个小时后感到很困惑,但问题太多了。也许你可以帮助我: - )

我希望有一个Pivot和绑定元素,所以我编写了一个包含ObservableCollection(CategoryPageList)的PivotViewModel类:

public class PivotViewModel
{
    public class CategoryPageList : ObservableCollection<CategoryPage>
    {}

    private CategoryPageList categoryPages;

    public PivotViewModel()
    {
        categoryPages = new CategoryPageList();
    }

    public CategoryPageList CategoryPages
    {
        get { return categoryPages; }
    }
}

CategoryPageList属于CategoryPage类型,一个包含我要绑定的数据的类,即“CategoryName”:

public class CategoryPage : INotifyPropertyChanged
{
    private string categoryName;

    public CategoryPage(string categoryName)
    {
        CategoryName = categoryName;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public string CategoryName
    {
        get { return categoryName; }
        set
        {
            if (categoryName != value)
            {
                categoryName = value;
                NotifyPropertyChanged("CategoryName");
            }
        }
    }
}

加载我的应用程序时,我希望为每个CategoryPage看到一个PivotItem,并将“CategoryName”-Property作为此PivotItem的标题。 使用Expression Blend,我创建了以下XAML代码,但它不起作用,Pivot仍为空:

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.DataContext>
        <RssReader_ViewModel:PivotViewModel/>
    </Grid.DataContext>
    <controls:Pivot Title="pivot" ItemsSource="{Binding CategoryPages}">
        <controls:Pivot.Resources>
            <DataTemplate x:Key="CategoryPivotItemHeaderTemplate">
                <TextBlock TextWrapping="Wrap" Text="{Binding CategoryPages[0].CategoryName}"/>
            </DataTemplate>
            <DataTemplate x:Key="CategoryPivotItemTemplate">
                <Grid>
                    <ListBox/>
                </Grid>
            </DataTemplate>
        </controls:Pivot.Resources>
        <controls:Pivot.ItemTemplate>
            <StaticResource ResourceKey="CategoryPivotItemTemplate"/>
        </controls:Pivot.ItemTemplate>
        <controls:Pivot.HeaderTemplate>
            <StaticResource ResourceKey="CategoryPivotItemHeaderTemplate"/>
        </controls:Pivot.HeaderTemplate>
        <controls:Pivot.DataContext>
            <RssReader_ViewModel:PivotViewModel/>
        </controls:Pivot.DataContext>
    </controls:Pivot>
</Grid>

DataContext绑定在Page:

的构造函数中的代码中
public partial class MainPage : PhoneApplicationPage
{
    public MainPage()
    {
        InitializeComponent();

        LayoutRoot.DataContext = App.PivotViewModel;
    }
}

我是一个初学者并且没有太多想法,但我唯一可以说的是,在{XAML中Text="{Binding CategoryPages[0].CategoryName}"看起来很奇怪。

有人看到错误吗?会真的很棒! 最好的问候!

2 个答案:

答案 0 :(得分:1)

尝试将空/假DataContext="{Binding}"插入数据透视标记。

删除CategoryPages[0],因为您无权访问CategoryPages集合。

答案 1 :(得分:0)

尝试删除CategoryPages [0]。