Apr 24

admin

WPF ListView with GridView

wpf

In this article we'll talk about using the ListView but with one of it's most useful feature: the GridView. With this functionality we're able to present data to users in the form of tables and rows, just like a data grid would do in the old Windows Forms space.

Below you can find a pretty simple example of a list view with three columns:


    
        
            
                
                    
                    
                    
                
            
        
    

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;
        }
    }
}

In this sample, we have a ListView with three simple columns. We're just changing the default View for the list view to be GridView (this is the only one available but you can create your own). We then just define the columns using the XAML markup as shown above. In each column we're defining the Header that's shown at the top of the grid, the width and the DisplayMemberBinding that binds the value of the column to the actual object we're using in the C# code.

Using templates as cell content

We can add our own controls to grid view columns using the CellTemplate property in WPF. This give us the option to render the content of the columns in any way that we need instead of just displaying strings as the DisplayMemberBinding property does.

In the sample below, we'll be changing the url column to display what an actual link looks like by using the TextBlock element:


    
        
            
            
        
        
            
                
                    
                    
                        
                            
                                
                            
                        
                    
                    
                
            
        
    

As you can see, this very simple. In the middle columns we just specified that we wanted a CellTemplate to be used, define what we wanted as a template (in this case a TextBlock) and just let the control know how to bind to the property. You can use this same approach to basically use any other element as a CheckBox for example as shown in the ListView checkbox sample.

No Comments

Add Comment