I'm using dapper to retrieve some data from a database using a list. I've been able to do it with a ListBox using
private void UpdateBinding()
{
displayLastNameLB.ItemsSource = people;
displayLastNameLB.DisplayMemberPath = "FullInfo";
}
and now I want to display it inside a TextBlock but I can't find how. What would be the text block equivalent of ItemSource
and DisplayMemberPath
?
Here is the full code.
public partial class MainWindow : Window
{
List<Person> people = new List<Person>();
public MainWindow()
{
InitializeComponent();
UpdateBinding();
}
private void UpdateBinding()
{
displayLastNameLB.ItemsSource = people;
displayLastNameLB.DisplayMemberPath = "FullInfo";
}
private void Search_Click(object sender, RoutedEventArgs e)
{
DataAccess db = new DataAccess();
people = db.GetPeople(lastNameTB.Text);
UpdateBinding();
}
}
The Person List
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string FullInfo
{
get
{
return $"{ FirstName } { LastName } ({ PhoneNumber })";
}
}
public string FirstNameOnly
{
get
{
return $"{ FirstName }";
}
}
}
Just replace the ListBox
with a TextBlock
in the XAML and set the Text
property:
public partial class MainWindow : Window
{
List<Person> people = new List<Person>();
public MainWindow()
{
InitializeComponent();
UpdateBinding();
}
private void UpdateBinding()
{
if (people != null && people.Count > 0)
{
textBlock.Text = people[0].FullInfo;
}
}
private void Search_Click(object sender, RoutedEventArgs e)
{
DataAccess db = new DataAccess();
people = db.GetPeople(lastNameTB.Text);
UpdateBinding();
}
}