Apr 24

admin

WPF ListView Binding

wpf

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:


    
        
            
                
                    
                    
                    
                
            
        
    

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
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List businesses = new List();
            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;
           
        }
    }
}

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
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            ObservableCollection businesses = new ObservableCollection();
            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;
           
        }
    }
}

No Comments

Add Comment