开发者

Lost in the listbox data binding

I am trying to do something which I thought would be really simple, but.. I have created a pivot application and inserted a listbox in MainPage.xaml

< ListBox x:Name = "partyList" Margin = "0,0,-12,0" > 
< ListBox.ItemTemplate > 
    < DataTemplate > 
        < StackPanel Orientation = "Horizontal" Margin = "0,0,0,17" > 
            < StackPanel Width = "311" > 
                < TextBlock Text = "{Binding throwLocation}" 
                           TextWrapping = "Wrap" 
                  Style = "{StaticResource PhoneTextExtraLargeStyle}" /> 
                < TextBlock Text = "{Binding throwText}" 
                            TextWrapping = "Wrap" 
                         开发者_Go百科   Margin = "12,-6,12,0" 
                Style = "{StaticResource PhoneTextSubtleStyle}" /> 
            </ StackPanel > 
        </ StackPanel > 
    </ DataTemplate > 
 </ ListBox.ItemTemplate > 
</ ListBox > 

I want to fill something in that listbox.. and have created an ObservableCollection in mainpage.xaml.cs and figured I could just point ItemsSource to that, but nothing shows up in the list.

    public class ListItems
    {
        public string throwText;
        public string throwLocation;
    }

    List<ListItems> listItems = new List<ListItems>();
    public ObservableCollection<ListItems> oblItems = 
                      new ObservableCollection<ListItems>();

    // Load data for the ViewModel Items
    private void MainPage_Loaded(object sender, RoutedEventArgs e)
    {

        ListItems li = new ListItems();
        //li.thumb = new BitmapImage();

        li.throwLocation = "Denmark. Åbenrå";
        li.throwText = "Throw text";
        oblItems.Add(li);

        partyList.DataContext = oblItems; // Don't know if this makes any sense? 
        partyList.ItemsSource = oblItems;

        MessageBox.Show(oblItems[0].throwLocation);
    }

I get the messagebox and I can see that the data has reached the oblItems collection, but nothing in the listbox.

What I am doing wrong? I thought this should be quite simple.


OK, the answer to your question comes in two parts, one the reason why your binding fails and then a tip. The reason your data doesn't display is because the two properties in your "ListItems" class are not declared properly, they need to be fully declared properties using Getters and Setters, like this:

    public class ListItems
{
    public string throwText  { get; set; }
    public string throwLocation { get; set; }
}

Simply put unless you expose the values properly then Silverlight is unable to Bind and request the data properly.

Now for a hint If you fire up the DataBound template with the Windows Phone SDK you will see a better way of doing this by using MVVM (a framework fro proper data binding) where you separate out the view (the XAML), the Model (what you data looks like, e.g. your properties) and the data. MVVM is a more data / type safe way of designing applications that display data through data binding. Once you've looked through that I also suggest looking at frameworks such as MVVMLight (http://mvvmlight.codeplex.com) or Calburn.Micro (http://caliburnmicro.codeplex.com)to set you in good stead for the future.

Hope this helps.


You need to call databind on the list: partyList.DataBind(). This is what cUses the items to actually be added to the list.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜