windows phone 8绑定列表里面的列表框

时间:2013-10-26 08:46:48

标签: xaml windows-phone-7 windows-phone-8 windows-phone windows-phone-7.1

如何将排名RankingInfo1和rankinginfo 2绑定到列表框。基本上我遇到了三级层次结构的问题。 RankingInfo1 - > RankingInfo - >排名。

public class BookInfo
{
    private long _BookId = 0;
    public long BookId
    {
        get
        {
            return _BookId;
        }
        set
        {
            _BookId = value;

        }
    }     

    private string _BookTitle = string.Empty;
    public string BookTitle
    {
        get
        {
            return _BookTitle;
        }
        set
        {
            _BookTitle = value;

        }
    }

    public List<RankInfo> RankingInfo1 { get; set; }
    public List<RankInfo> RankingInfo2 { get; set; }

}

public class RankInfo {
    public int? Ranking { get; set; }
    public DateTime? WeekDate { get ; set ;}
}

我尝试了但事情是我得到了:booklocator.Model.RankInfo作为输出。

 <ListBox  x:Name="lstrankingUSAToday" ItemsSource="{Binding RankingInfo1}" Grid.ColumnSpan="2">
                            <StackPanel Width="Auto">
                                <TextBlock Text="Ranked USA Today: "/>
                                <TextBlock Text="{Binding Ranking}"></TextBlock>
                            </StackPanel>
                        </ListBox>

1 个答案:

答案 0 :(得分:2)

您需要使用ItemsControl来绑定值列表。我正在展示简单的演示尝试它,如果你需要进一步的帮助,请告诉我。

XAML

<ListBox ItemsSource="{Binding Items}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding BookId}" FontSize="20" />
                <TextBlock Text="{Binding BookTitle}" FontSize="20" />
                <ItemsControl ItemsSource="{Binding RankingInfo1}" Margin="0 20 0 0">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Border BorderBrush="Blue" BorderThickness="2">
                                <TextBlock Text="{Binding Ranking}" FontSize="20" />
                            </Border>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
                <ItemsControl ItemsSource="{Binding RankingInfo2}" Margin="0 20 0 0">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Border BorderBrush="Red" BorderThickness="2">
                                <TextBlock Text="{Binding Ranking}" FontSize="20" />
                            </Border>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

C#

public List<BookInfo> Items { get; set; }

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    var _RankingInfo1 = new List<RankInfo> 
    { 
        new RankInfo { Ranking = 1, WeekDate = DateTime.Now },
        new RankInfo { Ranking = 2, WeekDate = DateTime.Now }
    };

    var _RankingInfo2 = new List<RankInfo> 
    { 
        new RankInfo { Ranking = 10, WeekDate = DateTime.Now.AddDays(-2) },
        new RankInfo { Ranking = 20, WeekDate = DateTime.Now.AddDays(-2) }
    };

    Items = new List<BookInfo> 
    { 
        new BookInfo { BookId = 9, BookTitle = "My Book Title", RankingInfo1 = _RankingInfo1, RankingInfo2 = _RankingInfo2 }
    };

    this.DataContext = this;
}