Introducing XDA:DevCon – A Conference For Developers By Developers
XDA Developers Android and Mobile Development Forum
Forgot your password?
 
Post Reply+
Tip us?
 
Robby Light
Old
#1  
Robby Light's Avatar
Member - OP
Thanks Meter 1
Posts: 37
Join Date: May 2010
Default [Q] ListBox Binding Error with Observable Collection

Hi all!

Since days I have a problem now. I have an app that manage Entries in a list A. If one of these entries End-Date is today I whant that entry to be shown in a second list B. This is checked when I return from the "addNewItem" Window so I use onNavigatedTo.

Code:
// Static Variables
        public static ObservableCollection<Item> lstToday = new ObservableCollection<Item>();
        public static ObservableCollection<Item> lstWeek = new ObservableCollection<Item>();
        public static ObservableCollection<Item> lstAll = new ObservableCollection<Item>();

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            MessageBox.Show("Initialize Component");
            lbToday.ItemsSource = lstToday;
            lbWeek.ItemsSource = lstWeek;
            lbAll.ItemsSource = lstAll;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            DateTime currentDate = DateTime.Now;
            foreach (Item item in lstAll)
            {
                if (item.endDate.ToString("yyyy'-'MM'-'dd").Equals(currentDate.ToString("yyyy'-'MM'-'dd")))
                { 
                    lstToday.Add(item);
                    MessageBox.Show("Item '" + item.name + "' added to Todaylist");
                }
            }
        }

        private void appBarNew_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri(string.Format("/EditAddItem.xaml"), UriKind.Relative));
        }
After that I get an "System.Diagnostics.Debugger.Break();" error that doesn't occur when I delete "lbToday.ItemsSource = lstToday;" to avoid the ListBox Binding.

With lbAll ist runs without any problems!
Can I bind the Listbox direct to the ObservableCollection in XAML=
I really don't know, whats to do. So do you?

It would make my day!
Thanks!
 
otherworld
Old
(Last edited by otherworld; 28th April 2011 at 01:23 PM.)
#2  
Senior Member
Thanks Meter 13
Posts: 130
Join Date: Apr 2008
Location: Leeds
What is the exception message and stack trace when the exception is thrown? (you can browse the properties of the exception using visual studio).

Your code actually works for me (although I had to make some assumptions about what is in lstAll as you don't mention that, and you may have an error in the DataTemplate for the listbox for binding your Item class)

Things to try:

Have you tried it with non static observable collections?

Use binding assignments in the xaml rather than setting the itemssource directly (e.g. using a viewmodel). Then you can let the phone engine worry about when to do the assigments. If you do that don't forget to set the page's datacontext property.

Try it with simple collections of strings first (rather than binding an 'item' class) so you can check it's working without a datatemplate.

Hope you fix it.

Ian
Developer of the toasty live-tiled and multi-league football app Premier Leagues +
 
Post Reply+
Tags
binding, collection, listbox, problem, xaml
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Go to top of page...