Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.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
如何在silverlight中将集合类用作静态资源_Silverlight_Binding_Path_Itemssource_Staticresource - Fatal编程技术网

如何在silverlight中将集合类用作静态资源

如何在silverlight中将集合类用作静态资源,silverlight,binding,path,itemssource,staticresource,Silverlight,Binding,Path,Itemssource,Staticresource,我有一个名为Customer的简单类,它有两个属性。 public Name{get;set;}public LastName{get;set} 然后我创建了一个名为CustomerList的集合类,其中只有一个名为Customers的公共属性 public class CustomerList { public List<Customer> Customers { get; set; } public CustomerList() { Cu

我有一个名为
Customer
的简单类,它有两个属性。
public Name{get;set;}
public LastName{get;set}

然后我创建了一个名为
CustomerList
的集合类,其中只有一个名为Customers的公共属性

public class CustomerList
{
    public List<Customer> Customers { get; set; }

    public CustomerList()
    {
        Customers = new List<Customer>();
        Customers.Add(new Customer() { Name = "Foo", LastName = "Bar" });
        Customers.Add(new Customer() { Name = "Foo1", LastName = "Bar1" });
    }
}
公共类客户列表
{
公共列表客户{get;set;}
公共客户列表()
{
客户=新列表();
添加(新客户(){Name=“Foo”,LastName=“Bar”});
添加(新客户(){Name=“Foo1”,LastName=“Bar1”});
}
}
现在我想在XAML中将这个类用作静态资源

  <UserControl.Resources> 
  <customers:CustomerList x:Key="CustomersKey">
  </UserControl.Resources>

然后在列表框中使用它

 <ListBox x:Name="lvTemplate" ItemsSource="{Binding Source={StaticResource CustomersKey}}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBox Text="{Binding Name}"/>
                    <TextBox Text="{Binding LastName}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

如果我在后面的代码中设置ItemsSource,在实例化该类之后,所有这些都可以正常工作。如果我尝试从XAML和静态资源设置它,则什么也不会发生。即使我使用
{Binding Path=Customer.Name}
{Binding Path=Name}
也不行


显然我遗漏了一些东西…

因为CustomerList实际上不是项列表(不实现IEnumerable),所以需要指定对象中要用作项资源的属性

<ListBox ItemsSource="{Binding Path=Customers, Source={StaticResource CustomersKey}}">

哦!我试图使用DataTemplate中的路径。。。这是我的错误。谢谢你的回答。