i'm trying to Bind an ObservableCollection to an ItemsControl. I created a class called Persons with some properties. Then I created a ObservableCollection and add some Persons in there.
If I give the ItemsControl a x:Name="PersonHolder" and add the ItemsSource with PersonHolder.ItemsSource = persons then all works. But if I'm trying to add the persons with a Binding in XAML... no result.
Here is the XAML:
<StackPanel Background="White">
<ItemsControl ItemsSource="{Binding persons}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
Some code from C#
public ObservableCollection<Person> persons { get; set; }
public MainWindow()
{
InitializeComponent();
persons = new ObservableCollection<Person>();
persons.Add(new Person { Name = "Peter" });
}
public class Person
{
public string Name { get; set; }
}
Hope you can help me.
Best regards.