棘手的WPF绑定

Coo*_*raw 2 data-binding wpf binding listbox windows-phone-7

我无法在Silverlight 4中进行简单但棘手的WPF绑定(WP7开发)

我有以下代码:

Class People{
    public string firstname;
    public string lastname;
}

Class DataSource{
    public static List<People> people; // consider this as a list filled with objects already
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试将人员列表放入ListBox,这是我尝试过的xaml:

            <ListBox x:Name="peoplelistbox" Margin="0,0,-12,0" ItemsSource="{Binding DataSource.people}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                      <StackPanel Margin="0,0,0,17" Width="432">
                            <TextBlock Text="{Binding firstname}" TextWrapping="Wrap"/>
                            <TextBlock Text="{Binding lastname}" TextWrapping="Wrap"/>
                      </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
Run Code Online (Sandbox Code Playgroud)

但不幸的是我的列表框仍然是空的.我究竟做错了什么 ?

先感谢您 :)

干杯,米卢德B.

Yog*_*esh 5

首先,您使用的是字段,您应该使用public属性(即人员,名字和姓氏).转换people为公共属性,如下所示:

public static List<People> people { get; set; }
Run Code Online (Sandbox Code Playgroud)

然后,您需要绑定ItemsSource使用x:Static标记,如下所示:

<ListBox x:Name="peoplelistbox" Margin="0,0,-12,0">
   <ListBox.ItemsSource>
      <Binding Source="{x:Static local:DataSource.people}"/>
   <ListBox.ItemsSource/>
   ...
Run Code Online (Sandbox Code Playgroud)

PS:local是指向DataSource类的命名空间的xml命名空间.此外,您的课程也需要成为公共课程.

编辑:
对于WP7,您需要在资源中声明类的实例,然后您可以使用Path指向源.像这样:

<phone:PhoneApplicationPage.Resources>
    <local:DataSource x:Key="dataSource"/>
</phone:PhoneApplicationPage.Resources>
...
<ListBox x:Name="peoplelistbox" Margin="0,0,-12,0" ItemsSource="{Binding Source={StaticResource dataSource}, Path=people}">
Run Code Online (Sandbox Code Playgroud)

PS:同样,你的类需要是公共的,并且必须有一个默认的构造函数.

编辑:
这是一个在我的系统上完美运行的例子.检查并查看您在哪里犯错误:

namespace WindowsPhoneApplication1
{
    public class People
    {
        public string firstname { get; set; }
        public string lastname { get; set; }
    }

    public class DataSource
    {
        public static List<People> people { get; set; }

        public DataSource() { }

        static DataSource()
        {
            people = new List<People> {new People {firstname = "Foo", lastname = "Bar"}};
        }
    }

    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Xaml(仅相关部分):

...
...
xmlns:local="clr-namespace:WindowsPhoneApplication1"
...
...
<phone:PhoneApplicationPage.Resources>
    <local:DataSource x:Key="dataSource"/>
</phone:PhoneApplicationPage.Resources>
...
...
<ListBox x:Name="peoplelistbox" Margin="0,0,-12,0" ItemsSource="{Binding Source={StaticResource dataSource}, Path=people}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Margin="0,0,0,17" Width="432">
                <TextBlock Text="{Binding firstname}" TextWrapping="Wrap"/>
                <TextBlock Text="{Binding lastname}" TextWrapping="Wrap"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)