WPF

WPF ListView Binding

Jose Leon
Author

Binding in the ListView will allow you to get information from a database or a list and bind that information to controls. This way, you don't need to manually insert the data into the controls, the ListView will do this automatically.

Below you can an example in XAML on how to use binding:

<window x:class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="MainWindow" height="350" width="525">

   <grid>

       <listview margin="10" name="BusinessListView">

           <listview.view>

               <gridview>

                   <gridviewcolumn header="Company" width="120" displaymemberbinding="{Binding Company}"></gridviewcolumn>

                   <gridviewcolumn header="Url" width="200" displaymemberbinding="{Binding Url}"></gridviewcolumn>

                   <gridviewcolumn header="Phone" width="100" displaymemberbinding="{Binding Phone}"></gridviewcolumn>

               </gridview>

           </listview.view>

       </listview>

   </grid>

</window>

What we have here is a ListView showing information as a gird. Each column in the grid is bind to a property and this is done using the DisplayMemberBinding attribute. This is indicating to which property in the dataset or object the column maps to.

Binding multiple columns

Binding to multiple columns in the ListView is also pretty straightforward. In the previous XAML sample we're actually binding to more than one column. By adding the C# code below, we can populate this ListView:

namespace WpfApplication2

{

   /// <summary>

   /// Interaction logic for MainWindow.xaml

   /// </summary>

   public partial class MainWindow : Window

   {

       public MainWindow()

       {

           InitializeComponent();

           List<business> businesses = new List<business>();

           businesses.Add(new Business("Microsoft", "http://www.microsoft.com", "123-421-1231"));

           businesses.Add(new Business("SkyXoft", "http://www.skyxoft.com","123-321-1231"));

           businesses.Add(new Business("LicenseSpot", "http://www.licensespot.com", "123-312-3212"));

           BusinessListView.ItemsSource = businesses;

           

       }

   }

   public class Business

   {

       public string Company { get; set; }

       public string Url { get; set; }

       public string Phone { get; set; }

       

       public Business(string company, string url, string phone)

       {

           this.Company = company;

           this.Url = url;

           this.Phone = phone;

           

       }

   }

}

</business></business>

What we're doing is create a List of Business objects and filling with information. Each Business object has a Company, Url and Phone property that maps to the bindings defined in the XAML markup. When we assign it to the ItemsSource property in the ListView, it'll be automatically filled:

Binding and SelectedItems

For getting the selected items in the ListView when using binding, we simply need to use the SelectedItems property of the ListView and loop through it. The following code shows an example:

foreach (Business business in BusinessListView.SelectedItems)

{

    MessageBox.Show(business.Company);

}

Notice that when we do the for each, the SelectedItems actually holds the objects binded to the ListView, so a cast is automatically done and we can interact with the object right away.

Binding and ObservableCollection

Binding to an ObjservableCollection is also pretty straightforward. In the sample below we're changing the value of the Company propety for the first item in the List. When we run the project, you can see that the value is changed on the ListView and we didn't have to rebind it or do any more coding:

namespace WpfApplication2

{

   /// <summary>

   /// Interaction logic for MainWindow.xaml

   /// </summary>

   public partial class MainWindow : Window

   {

       public MainWindow()

       {

           InitializeComponent();

           ObservableCollection<business> businesses = new ObservableCollection<business>();

           businesses.Add(new Business("Microsoft", "http://www.microsoft.com", "123-421-1231"));

           businesses.Add(new Business("SkyXoft", "http://www.skyxoft.com","123-321-1231"));

           businesses.Add(new Business("LicenseSpot", "http://www.licensespot.com", "123-312-3212"));

           BusinessListView.ItemsSource = businesses;

           businesses[0].Company = "Microsoft Corp";

           

       }

   }

   public class Business

   {

       public string Company { get; set; }

       public string Url { get; set; }

       public string Phone { get; set; }

       

       public Business(string company, string url, string phone)

       {

           this.Company = company;

           this.Url = url;

           this.Phone = phone;

           

       }

   }

}

</business></business>

                   

features

All posts

  • licensing
  • analytics

Easy .NET Protection

Purchase template