Question

How can i bind ranking RankingInfo1 and rankinginfo 2 to the listbox. basically i am having trouble with the three level hierarchy. RankingInfo1 -> RankingInfo -> Ranking.

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 ;}
}

what i have tried but the thing is that i am getting : booklocator.Model.RankInfo as the output.

 <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>
Was it helpful?

Solution

You need to use ItemsControl to bind the list of values. I am showing simple demo try it and let me if you need further help.

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top