I have a simple class named Customer
with 2 properties.public Name {get;set;}
public LastName {get;set}
Then I made a collection class named CustomerList
with only one public property named 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" });
}
}
Now I want to use this class as a static resouce in XAML.
<UserControl.Resources>
<customers:CustomerList x:Key="CustomersKey">
</UserControl.Resources>
and then use it in a 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>
if I set the ItemsSource in code behide, after instantiating the class, all work fine. If I try to set it from XAML and the static resource Nothing happens. not even if I use the {Binding Path=Customer.Name}
or {Binding Path=Name}
.
Clearly I miss something...
Since the CustomerList isn't actually the list of items (does not implement IEnumerable), you need to specify what property inside the object you want to use as ItemsSource.
<ListBox ItemsSource="{Binding Path=Customers, Source={StaticResource CustomersKey}}">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With