I was just starting to learn WPF, and I got a little confused in some areas related to data binding. I have no problem with the syntax, but most likely some newbie errors have been made, and I have a couple of questions.
I made a simple screen with 2 text fields, and when I click the button, these two elements are added to the ListBox.
Link in the Window tag XAMLto the People class
xmlns:classes="clr-namespace:WPF_Course.Classes"
Window resource added
<Window.Resources>
<classes:People x:Key="people"/>
</Window.Resources>
This is how I announced my Listbox
<ListBox DataContext="{Binding Source={StaticResource people}}"
ItemsSource="{Binding Persons}"
x:Name="PersonListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<StackPanel>
<TextBlock Text="{Binding FullName}"/>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
So, I added DataContexta ListBox to my list, where I bind it to the resource of my people, and also add ItemSourcethat looks at the property in mine People.
This is my class
public class People : ObservableCollection<Person>
{
public ObservableCollection<Person> Persons { get { return persons; } set { persons = value; } }
private ObservableCollection<Person> persons = new ObservableCollection<Person>();
public People()
{
for (int i = 0; i < 1; i++)
{
Persons.Add(new Person()
{
Name = "Dummy",
LastName = "Dummy",
Age = 15
});
}
}
}
Based on what I have done so far, I have the following questions:
1) ( , ?)
ItemsSource = "{Binding Persons}"
ItemsSource = "{Binding Path = Persons }"
, ItemSource = "{Binding}" am, a People, , ? , , , , .
2) Peoples ObservableCollection<Person> ( Person ). , , (ObservableCollection<person> type ), ( ), , , ?, :
, ( , - ), ObservableCollection<myClass> , ? ( )
, , , , wpf.
2: , . , . ( , , , , )
ObservableCollection<Person> ppl;
public MainWindow()
{
InitializeComponent();
person = new Person();
stackPanelPerson.DataContext = person;
people = new People();
ppl = people.Persons;
PersonListBox.ItemsSource = ppl;
}
ppl.Add(new Person() { Name = boxFirstName.Text, LastName = boxLastName.Text, Age = Int32.Parse(boxAge.Text) });
, Person (INotifyPropertyChanged) , :
ppl.Add(new Person() { Name = person.Name, LastName = person.LastName, Age = person.Age});
!!
!