Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello All,

I am trying to put together a small TV Guide Application and I am having some trouble Binding to the queried information.

The plan is to have a ListBox where each Item contain an Image and/or the Channel Name and another ListBox (just to confirm, that's a ListBox in a ListBox Item) which will contain all the upcoming TV programs for that channel.

I found something very similar to what I'd like to do but I can't seem to get it to work, can anybody see what I'm doing wrong?

XML
<ListBox x:Name="TVGuideListBox">
 <ListBox.ItemTemplate>
  <DataTemplate>
   <StackPanel>
    <DockPanel>
     <TextBlock MinWidth="160" Text="{Binding Path=Channel.ChannelName}" DockPanel.Dock="Left" />
       <ListBox ItemsSource="{Binding Path=Program}" BorderThickness="0">
                                <ListBox.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel Orientation="Horizontal" IsItemsHost="True"/>
                                    </ItemsPanelTemplate>
                                </ListBox.ItemsPanel>
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                            <TextBlock Text="{Binding Path=Title}" />
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>
                        </DockPanel>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
</ListBox>


C#
namespace TVGuide
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 


    public partial class MainWindow : Window
    {
        TVDataContext TVDC = new TVDataContext();

        public MainWindow()
        {
            InitializeComponent();
            LoadChannels();
        }

        public class Channel
        {
            public string ChannelName { get; set; }
            public string ChannelID { get; set; }
        }

        public class Program
        {
            public string StartTime { get; set; }
            public string StopTime { get; set; }
            public string ChannelID { get; set; }
            public string Title { get; set; }
            public string Description { get; set; }
        }

        public class exChannels
        {
            public List<Channel> ChannelInfo { get; set; }
            public List<Program> ProgramInfo { get; set; }

            public exChannels(List<Channel> ch, List<Program> prog)
            {
                ChannelInfo = ch;
                ProgramInfo = prog;
            }
        }

        public void LoadChannels()
        {
            DateTime time = DateTime.Now.ToUniversalTime();              // Use current time
            string format = "yyyy MM d HH mm";    // Use this format
            string timevalue2 = Regex.Replace(time.ToString(format), @"[\D]", "");
            decimal currenttime_dec = (Convert.ToDecimal(timevalue2) * 100);

            XDocument xmlDoc = XDocument.Load(@"C:\iceguide.xml");

            var ChannelsQuery = (from s in TVDC.tv_CHANNELs
                                 orderby s.channel_ID
                                 select new Channel
                                 {
                                     ChannelName = s.channel_NAME,
                                     ChannelID = s.guide_ID
                                 }).ToList<Channel>();

            foreach (var s in ChannelsQuery)
            {
                var ProgramQuery = (from c in xmlDoc.Descendants("programme")
                                    where c.Attribute("channel").Value == s.ChannelID
                                    select new Program
                                    {
                                        StartTime = c.Attribute("start").Value,
                                        StopTime = c.Attribute("stop").Value,
                                        ChannelID = c.Attribute("channel").Value,
                                        Title = c.Element("title").Value,
                                        Description = c.Element("desc").Value
                                    }).ToList<Program>();

                exChannels exChannel = new exChannels(ChannelsQuery, ProgramQuery);

                TVGuideListBox.Items.Add(exChannel);
            }

        }

        
    }
}



Am I barking up the wrong tree? Any help or even a kick in the right direction would be greatly appreciated.

Thank you in advance,

Kind Regards,
Alex
Posted

1 solution

Instead of tryin to put a ListBox inside another ListBox, you should rather Group the items in one ListBox by Channel. Search for how to group items in a ListBox using a CollectionViewSource. ListBox Grouping, Sorting, Subtotals and Collapsible Regions[^] is agood start. Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900