У меня не получается добавить элемент в ListBox по нажатию кнопки:
Класс элемента:
public class People
{
public string FirstName {}
public string LastName {}
}
Это лист:
private List<People> itemsList = new List<People>();
public List<People> ItemsList
{
get { return itemsList; }
}
Команда для кнопки:
AddCommand = new Command(arg => AddMethod());
private void AddMethod()
{
if (TextProperty1 == "" || TextProperty2 == "")
{MessageBox.Show("Please write all fields"); return;}
itemsList.Add(new People(TextProperty1, TextProperty2));
OnPropertyChanged(nameof(ItemsList));
}
Код листбокса:
<ListBox ItemsSource="{Binding ItemsList, UpdateSourceTrigger=PropertyChanged}" HorizontalContentAlignment="Stretch" Margin="5"
VerticalAlignment="Top" Height="300" Width="750" Grid.Row="1">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="0,2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding LastName}"/>
<TextBlock Text="{Binding FirstName}" Grid.Column="1"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>