Lorena Drăgan
Lorena Drăgan

Reputation: 3

Bind result from a Web API Projection request to a WPF Control like DataGrid or ListView

I am trying to write the Client part of a Web API and to consume those requests in a user-friendly mode. I wrote the controller for the server part and now I am trying to display queried data.

With projection, I can't use the EDM type(Student) because I do not know what attributes the user will request, so I managed to use ExpandoObject an add to it only those properties the Client checks in the UI

Now ExpandoObject implements the IDictionary< string, object > interface from where you can access each dynamic property.

Is there any way I could populate a ListView or a DataGrid from the type IEnumerable< IDictionary< string,object>> returned from the Web API method? (IEnumerable because i have a List of students generated from a database) This is how I get only the requested columns:

   return db.Students.AsEnumerable().Select(student =>
        {
            IDictionary<string, object> expandoStudent = new ExpandoObject();

            foreach (var property in properties)
                expandoStudent.Add(property.Name, property.GetValue(student, null));

            return expandoStudent;
        });

Upvotes: 0

Views: 190

Answers (1)

Anton Polchenko
Anton Polchenko

Reputation: 72

Yes you can populate a ListView or DataGrid

To do it more interactive way is better to use ObservableCollection as a source of items for your ListView this will update your UI when a new item been added to your items list.

And the actual DataBinding works similar with Xamarin DataBinding, it is just slightly different.

Here is the example:

public ListView()
{
    this.Student = new ObservableCollection();
    InitializeComponent();
    this.DataContext = this;

    // And here you actually add your items to the collection

    this.Student.Add(new IDictionary<string, object>("Key01", MyObject));
}

this refers to your ListView model.

And an ObservableCollection will run an event to update your UI after each new item you add to collection.

Upvotes: 0

Related Questions