I have a ListBox that shows a collection of Person objects with their full name. I also have 4 TextBoxes that shows all info (FirstName, LastName, Age, Phone) from a Person object when I click on a Person in the Listbox. This works fine.
I can change the values in the TextBoxes and they will be saved in the TexBoxes but it is not updating the name in the ListBox.
Iv´e tried to change the Binding property in Label Content From FullName to SelectedPerson.FullName but the Path for Label Content is PersonViewModel.
<ListBox ItemsSource="{Binding PersonCollectionVM}"
SelectedItem="{Binding SelectedPerson}"
Grid.Row="0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding FullName}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Grid Grid.Column="2">
<TextBox Text="{Binding SelectedPerson.FirstName, UpdateSourceTrigger=PropertyChanged}"
public class MainViewModel
{
private PersonRepository _personRepo;
private List<PersonViewModel> _personCollectionVM;
public MainViewModel()
{
_personRepo = new PersonRepository();
_personCollectionVM = new List<PersonViewModel>();
foreach (Person person in _personRepo.GetAll())
{
_personCollectionVM.Add(new PersonViewModel(person));
}
}
public List<PersonViewModel> PersonCollectionVM => _personCollectionVM;
public PersonViewModel SelectedPerson { get; set; }
}
public class PersonViewModel
{
private Person _person;
public PersonViewModel(Person person)
{
_person = person;
}
public string FirstName
{
get => _person.FirstName;
set => _person.FirstName = value;
}
public string LastName
{
get => _person.LastName;
set=> _person.FirstName = value;
}
public int Age
{
get => _person.Age;
set => _person.Age = value;
}
public string Phone
{
get => _person.Phone;
set => _person.Phone = value;
}
public string FullName => _person.FirstName + " " + _person.LastName;
}