Resolved DataGrid - can't get it to load with async method?

ConsKa

Well-known member
Joined
Dec 11, 2020
Messages
140
Programming Experience
Beginner
Using MVVM and Caliburn.Micro I was able to create a BindableCollection<Details> I was able to manually add something to this and put it into a DataGrid, by simply naming the DataGrid after the BindableCollection, code looked like this:

C#:
public BindableCollection<Details> Request { get; set; }

public UnusedViewModel()
{
    Request = new BindableCollection<Details>();
    
    Request.Add(new Details { ID = "Test"});
}
public class Details
{
    public string ID { get; set; }   
}

Now I have a Json that I load up, and I want to show the requests I get back, the code I have for the json is fine, just deserialised into a Json class...I couldn't get it to load into the datagrid, so in testing I wrote this:

C#:
Request = new BindableCollection<Detiails>();

foreach (var item in token.requests)
{
    Request.Add(new Details { ID = "test" })
}
}

In XAML I have:

C#:
<DataGrid x:Name="Request"/>

Which worked fine when I had the BindableCollection in the non-async method - and works fine if I make this methohd non-async (though I can't do the whole call to the API) but doesn't appear to work while in an async method?

I am a bit lost as to how to get this to work? If I make the method non-async, it binds fine, in async...nope nothing. I am totally bypassing the json, the collection has numerous results in it, it just isn't binding to the datagrid as it was in a non-async method.

Any insight gratefully received.
 
Solution
In your first code, you are setting the Request property in the constructor. So by the time WPF binds to the Request property, the collection is already instantiated and populated, and therefore has data to be displayed.

In your second code, you described it as living inside an async method. When WPF initially binds to the Request property, it is very likely null or an empty collection. Later when your async method gets executed, you create a new instance of a collection and population that collection. I don't see a property change notification in your Request property to tell WPF that you've changed the reference to a collection. So that means that WPF would still be bound to the null or empty collection...
In your first code, you are setting the Request property in the constructor. So by the time WPF binds to the Request property, the collection is already instantiated and populated, and therefore has data to be displayed.

In your second code, you described it as living inside an async method. When WPF initially binds to the Request property, it is very likely null or an empty collection. Later when your async method gets executed, you create a new instance of a collection and population that collection. I don't see a property change notification in your Request property to tell WPF that you've changed the reference to a collection. So that means that WPF would still be bound to the null or empty collection instead of the new collection.

When you said that you changed the async method over to a synchronous method, I am assuming that you are calling the method from within your constructor. So in that case, you are back again to the same state as the first code chunk where the collection is instantiated and populated before WPF performs the binding.
 
Solution
In your first code, you are setting the Request property in the constructor. So by the time WPF binds to the Request property, the collection is already instantiated and populated, and therefore has data to be displayed.

In your second code, you described it as living inside an async method. When WPF initially binds to the Request property, it is very likely null or an empty collection. Later when your async method gets executed, you create a new instance of a collection and population that collection. I don't see a property change notification in your Request property to tell WPF that you've changed the reference to a collection. So that means that WPF would still be bound to the null or empty collection instead of the new collection.

When you said that you changed the async method over to a synchronous method, I am assuming that you are calling the method from within your constructor. So in that case, you are back again to the same state as the first code chunk where the collection is instantiated and populated before WPF performs the binding.

Thanks Skydiver!

I had originally a full property for the BindableCollection....it was part of the MVVM / Caliburn Micro set up...I had stripped it away, actually created a nentirely new project to start from scratch, to test why the DataGrid was not populating.....now it is populating once I recreated the NotifyPropertyChange in the object.

Appreciate you taking the time to read that and spot the issue, much appreciated.
 
Back
Top Bottom