Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows phone 7 在listbox数据绑定中丢失_Windows Phone 7 - Fatal编程技术网

Windows phone 7 在listbox数据绑定中丢失

Windows phone 7 在listbox数据绑定中丢失,windows-phone-7,Windows Phone 7,我正在尝试做一些我认为非常简单的事情,但是。。 我已经创建了一个pivot应用程序,并在MainPage.xaml中插入了一个列表框 < ListBox x:Name = "partyList" Margin = "0,0,-12,0" > < ListBox.ItemTemplate > < DataTemplate > < StackPanel Orientation = "Horizontal" Margin = "0

我正在尝试做一些我认为非常简单的事情,但是。。 我已经创建了一个pivot应用程序,并在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" 
                            Margin = "12,-6,12,0" 
                Style = "{StaticResource PhoneTextSubtleStyle}" /> 
            </ StackPanel > 
        </ StackPanel > 
    </ DataTemplate > 
 </ ListBox.ItemTemplate > 
</ ListBox > 
我得到了messagebox,我可以看到数据已经到达了oblItems集合,但是列表框中没有任何内容


我做错了什么?我认为这应该很简单。

您需要调用列表中的数据绑定:partyList.databind()。这就是要实际添加到列表中的项目的原因。

好的,问题的答案分为两部分,一部分是绑定失败的原因,另一部分是提示。
    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);
    }
数据不显示的原因是“ListItems”类中的两个属性没有正确声明,它们需要使用getter和setter完全声明为属性,如下所示:

    public class ListItems
{
    public string throwText  { get; set; }
    public string throwLocation { get; set; }
}
简单地说,除非正确地公开这些值,否则Silverlight无法正确地绑定和请求数据

现在给你一个提示 如果您使用Windows Phone SDK启动数据绑定模板,您将看到一种更好的方法,即使用MVVM(一种用于适当数据绑定的框架),将视图(XAML)、模型(数据的外观,例如属性)和数据分开。 MVVM是设计通过数据绑定显示数据的应用程序的一种更加数据/类型安全的方法。 一旦你看完了,我还建议你看看MVVMLight之类的框架(http://mvvmlight.codeplex.com)或者卡尔伯恩,微型(http://caliburnmicro.codeplex.com)让你对未来有好处


希望这有帮助。

现在速度很快了!:-)但是partyList对象(Listbox)上没有这样的方法?您使用的Listbox的类型是什么?如果您浏览到partyList上的定义并让我知道。除了上述:字段不是绑定源规范的一部分,您至少需要绑定到属性。有关更多信息,请参见此处接受的答案