我有一个Customer
用 2 个属性命名的简单类。
然后我创建了一个集合类,命名只有一个名为 Customers 的公共属性
public Name {get;set;}
public LastName {get;set}CustomerList
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" });
}
}
现在我想将这个类用作 XAML 中的静态资源。
<UserControl.Resources>
<customers:CustomerList x:Key="CustomersKey">
</UserControl.Resources>
然后在 ListBox 中使用它
<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}
or 也不行{Binding Path=Name}
。
显然我错过了一些东西......