'WPF ListView not Binding to ViewModel Property
I'm trying to setup a listview that, after a call to the Azure GraphAPI will load a list of users. Everything up to the binding of the data works. The call to Graph works, the returned data works, all without issue.
I have a listview setup in a view:
<ListView ItemsSource="{Binding NoPicUsers}" >
<ListView.View>
<GridView>
<GridViewColumn
Header="Display Name"
Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding NoPicUsers.DisplayName}"></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
The corresponding ViewModel has the following code:
public class ContentPanelHomeViewModel : ViewModelBase
{
private List<UserModel> _noPicUsers;//NotifyTask<List<UserModel>> _noPicUsers;
public List<UserModel> NoPicUsers
{
get
{
return _noPicUsers;
}
set
{
_noPicUsers = value;
//OnPropertyChanged Method is in ViewModelBase
OnPropertyChanged("NoPicUsers");
}
}
public ContentPanelHomeViewModel()
{
//NoPicUsers = new NotifyTask<List<UserModel>>.Create(GetNoPicUsersAsync());
//NoPicUsers = NotifyTask.Create(GetNoPicUsersAsync);
var users = GetNoPicUsersAsync();
}
private async Task<List<UserModel>> GetNoPicUsersAsync()
{
GraphServiceClient graphClient = await GraphAPIServices.SignInAndInitGraph(AzureAppInfo.UserReadAll);
List<UserModel> users = new List<UserModel>();
var listOfUsers = await graphClient.Users
.Request()
.Filter("accountEnabled eq true")
.Select("displayname, id")
.GetAsync();
foreach (var user in listOfUsers)
{
UserModel azureUser = new UserModel();
azureUser.DisplayName = user.DisplayName;
azureUser.ObjectID = user.Id;
users.Add(azureUser);
}
NoPicUsers = users;
return users;
}
}
My listview does not display any data and I cannot figure out why. There are no reported errors and no binding errors reported when the WPF window loads so I'm not sure what would be the issue?
EDIT: I've updated my code. I'm implementing the NugetPackage Nito.Mvvm. I get the same problem of no data binding to my list view if I set my NoPicUsers inside of the GetNoPicUsersAsync() the WPF window yells saying NoPicUsers property no found on object of Type UserModel.
So it appears that it thinks my ViewModel as a UserModel property instead of a List property. What would cause that?
In stepping through the code the task never seems to complete. The Task sits at Status = WaitingForActivation, Method = "{Null}", Result = "{Not yet computed}".
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
