Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I data bind a list of strings to a ListBox in WPF/WP7?

I am trying to bind a list of string values to a listbox so that their values are listed line by line. Right now I use this:

<ListBox Margin="20" ItemsSource="{Binding Path=PersonNames}">     <ListBox.ItemTemplate>         <DataTemplate>             <StackPanel Orientation="Horizontal">                 <TextBlock Text="{Binding Path=Id}"></TextBlock>             </StackPanel>         </DataTemplate>     </ListBox.ItemTemplate> </ListBox> 

But I don't know what I am supposed to put into the textblock, instead of Id, since they are all string values, not custom classes.

Also it complains not having to find the PersonNames when I have it inside MainPage, as MainPage.PersonNames.

I set the data context to:

DataContext="{Binding RelativeSource={RelativeSource Self}}" 

I am doing it wrong?

like image 902
Joan Venge Avatar asked Feb 22 '12 08:02

Joan Venge


People also ask

Which class is used for data binding in WPF?

WPF data binding supports data in the form of CLR objects and XML. To provide some examples, your binding source may be a UIElement, any list object, a CLR object that is associated with ADO.NET data or Web Services, or an XmlNode that contains your XML data.

What is ItemsSource in WPF?

ItemsSource can be data bound to any sequence that implements the IEnumerable interface, although the type of collection used does determine the way in which the control is updated when items are added to or removed. When ItemsSource is set, the Items property cannot be used to control the displayed values.

How does data binding work in WPF?

Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.

How do I bind a list in WPF?

<ListView. View> <GridView> <GridViewColumn Header="Employee ID" DisplayMemberBinding="{Binding Path=EmployeeID}"/>


1 Answers

If simply put that your ItemsSource is bound like this:

YourListBox.ItemsSource = new List<String> { "One", "Two", "Three" }; 

Your XAML should look like:

<ListBox Margin="20" Name="YourListBox">     <ListBox.ItemTemplate>          <DataTemplate>              <StackPanel Orientation="Horizontal">                  <TextBlock Text="{Binding}" />              </StackPanel>          </DataTemplate>      </ListBox.ItemTemplate>  </ListBox>  

Update:

This is a solution when using a DataContext. Following code is the viewmodel you will be passing to the DataContext of the page and the setting of the DataContext:

public class MyViewModel {     public List<String> Items     {         get { return new List<String> { "One", "Two", "Three" }; }     } }  //This can be done in the Loaded event of the page: DataContext = new MyViewModel(); 

Your XAML now looks like this:

<ListBox Margin="20" ItemsSource="{Binding Items}">     <ListBox.ItemTemplate>         <DataTemplate>             <StackPanel Orientation="Horizontal">                 <TextBlock Text="{Binding}" />             </StackPanel>         </DataTemplate>     </ListBox.ItemTemplate> </ListBox> 

The advantage of this approach is that you can put a lot more properties or complex objects in the MyViewModel class and extract them in the XAML. For example to pass a List of Person objects:

public class ViewModel {     public List<Person> Items     {         get         {             return new List<Person>             {                 new Person { Name = "P1", Age = 1 },                 new Person { Name = "P2", Age = 2 }             };         }     } }  public class Person {     public string Name { get; set; }     public int Age { get; set; } } 

And the XAML:

<ListBox Margin="20" ItemsSource="{Binding Items}">     <ListBox.ItemTemplate>         <DataTemplate>             <StackPanel Orientation="Horizontal">                 <TextBlock Text="{Binding Path=Name}" />                 <TextBlock Text="{Binding Path=Age}" />             </StackPanel>         </DataTemplate>     </ListBox.ItemTemplate> </ListBox> 

Hope this helps! :)

like image 161
Abbas Avatar answered Sep 21 '22 00:09

Abbas