Are you missing a getter property when binding WPF grid to a Generic List?

I tasked myself couple of days ago to build a sample WCF Service to send a pre-configured auto response based on a specific request. 


I initially defined my Data Contract as follows:


namespace SampleWCFService

{
    [DataContract]
    public class Book
    {
        [DataMember]
        public string ISBN;


        [DataMember]
        public string Title;


        [DataMember]
        public string Author;
...
    }
}


The data contract was streamed over the wire with the code snippet below:



public List<Book> Filter(string Author, string Genre, string Title)
{
    XDocument db = GetData();


    List<Book> lstBooks = GetAllBooks();


    if (!string.IsNullOrEmpty(Author))
    {
      lstBooks = lstBooks.Where(book => book.Author == Author).ToList<Book>();
    }


    if (!string.IsNullOrEmpty(Genre))
    {
        lstBooks = lstBooks.Where(book => book.Genre == Genre).ToList<Book>();
    }


    if (!string.IsNullOrEmpty(Title))
    {
        lstBooks = lstBooks.Where(book => book.Title == Title).ToList<Book>();
    }


    return lstBooks;
}


[ServiceContract]
public interface ILibrary
{
    [OperationContract]
    List<Book> GetAllBooks();


    [OperationContract]
    List<Book> GetBookByISBN(string ISBN);


    [OperationContract]
    List<Book> Filter(string Author, string Genre, string Title);
}




Everything seemed fine until now.. the list of books were streamed over just fine! 

Now the fun part begins ...

I was all set to bind my WPF telerik's RadGridView when something just seem to not work - well, in fact, I ran into an issue - the grid displayed but with no Data !!




And don't get me thinking that I had something missing on the XAML (which is perhaps the most common thing I've seen around...)

  <telerik:RadGridView Name="dataGridView1" ItemsSource="{Binding}"  ...

I did some further digging into the call stack when I realised that ..hmm when the datagrid is getting bound to any list - it just can't figure out unless you supply a getter property for the data point it is retrieving.

Let me demonstrate that via the code snippet below:

        [DataMember]
        public string Author; // { get; set; }

        [DataMember]
        public string Description { get; set; }

and what we get on the UI is as below ...



Finally got the code straightened out as follows and everything turned out goody good :-)

public MainWindow()
{
    InitializeComponent();

    using (LibraryServiceReference.LibraryClient bookService = new LibraryServiceReference.LibraryClient())
    {
        dataGridView1.DataContext = bookService.Filter(Author: "", Title: "", Genre: "Fantasy");
    }
}

[DataContract]
public class Book
{
...

[DataMember]
public string Author { get; set; }

[DataMember]
public string Description { get; set; }


Have fun and remember don't forget the getter :-)

Comments

Popular posts from this blog

QuadKey - what it is?

Baseless merge!

Binding ENUM to WPF control - ComboBox