Установите или привяжите ItemsSource
ListBox
к ObservableCollection<Person>
и добавьте объект Person
к этому объекту в обработчике события щелчка:
public sealed partial class MainPage : Page
{
private readonly ObservableCollection<Person> _persons = new ObservableCollection<Person>();
public MainPage()
{
this.InitializeComponent();
listBox.ItemsSource = _persons;
}
private void RegistrerDebtor(object sender, RoutedEventArgs e)
{
Person person = new Person();
person.Identification = TxtIdentification.Text;
person.Name = TxtName.Text;
person.LastName = TxtLastName.Text;
person.Address = TxtAddress.Text;
person.Phone = TxtPhone.Text;
person.Email = TxtMail.Text;
person.Latitude = TxtLatitude.Text;
person.OriLat = CbxOrientationLat.Text;
person.Longitude = TxtLongitude.Text;
person.OriLon = CbxOrientationLon.Text;
_persons.Add(person);
}
}
Затем можно использоватьItemTemplate
для определения внешнего вида каждого Person
:
<ListBox x:Name="listBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
<TextBlock Text="{Binding LastName}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Если вы хотите отобразить сериализованную строку в ListBox
, вы можете просто изменить тип исходной коллекции и добавить strings
к нему вместо Person
объектов:
public sealed partial class MainPage : Page
{
private readonly ObservableCollection<string> _persons = new ObservableCollection<string>();
public MainPage()
{
this.InitializeComponent();
listBox.ItemsSource = _persons;
}
private void RegistrerDebtor(object sender, RoutedEventArgs e)
{
Person person = new Person();
person.Identification = TxtIdentification.Text;
person.Name = TxtName.Text;
person.LastName = TxtLastName.Text;
person.Address = TxtAddress.Text;
person.Phone = TxtPhone.Text;
person.Email = TxtMail.Text;
person.Latitude = TxtLatitude.Text;
person.OriLat = CbxOrientationLat.Text;
person.Longitude = TxtLongitude.Text;
person.OriLon = CbxOrientationLon.Text;
string json = JsonConvert.SerializeObject(person, Formatting.Indented);
_persons.Add(json);
}
}
Тогда вам не понадобится ItemTemplate
.