Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding Observable Collection in XAML in Windows Phone
    primarykey
    data
    text
    <p>I am having trouble with declaratively setting the ItemsSource of a ListBox contained within a PivotItem in a simple Windows Phone 7 page. I can successfully set the ItemsSource in code behind.</p> <p>Here is a snippet of the class that contains the ObservableCollection that I want to bind to:</p> <pre><code>sealed class Database : INotifyPropertyChanged { //Declare Instance private static readonly Database instance = new Database(); //Private Constructor private Database() { } //The entry point into this Database public static Database Instance { get { return instance; } } #region Collections corresponding with database tables public ObservableCollection&lt;Category&gt; Categories { get; set; } public ObservableCollection&lt;CategoryType&gt; CategoryTypes { get; set; } </code></pre> <p>And here is a sample of my XAML:</p> <pre><code>&lt;ListBox x:Name="CategoriesListBox" Margin="0,0,-12,0" ItemsSource="{Binding Categories}" DisplayMemberPath="Name" /&gt; </code></pre> <p>In my page I have tried setting the data context as follows:</p> <pre><code>this.DataContext = Database.Instance; </code></pre> <p>However the binding does not work unless I explicitly set the ItemsSource in code as follows:</p> <pre><code>CategoriesListBox.ItemsSource = Database.Instance.Categories; </code></pre> <p>I know that I should be able to do this all declaratively, however I have tried many different ways of setting the ItemsSource declaratively (in addition to what I have detailed above) and none work.</p> <p>Can someone help me out?</p> <p>Further info: The output windows at runtime shows the following: System.Windows.Data Error: Cannot get 'Categories' value (type 'System.Collections.ObjectModel.ObservableCollection`1[BTT.PinPointTime.Entities.Category]') from 'BTT.PinPointTime.WinPhone.Database' (type 'BTT.PinPointTime.WinPhone.Database'). BindingExpression: Path='Categories' DataItem='BTT.PinPointTime.WinPhone.Database' (HashCode=99825759); target element is 'System.Windows.Controls.ListBox' (Name='CategoriesListBox'); target property is 'ItemsSource' (type 'System.Collections.IEnumerable').. System.MethodAccessException: Attempt to access the method failed: BTT.PinPointTime.WinPhone.Database.get_Categories() at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload