I tried to bind ObservableCollection<UserProfile> UsersList
to pages DataContext
, but program keeps telling me, that he cannot find UsersList
although I can see it's values in OutPut using Debug.WriteLine
.
But on the other hand, if I add DataContext
in C# code, everything works perfectly.
What am I doing wrong?
C# code:
this.DataContext = new UsersViewModel(); //inside MainPage constructor
XAML code:
DataContext="{Binding UsersViewModel, RelativeSource={RelativeSource Self}}" //inside <page .../>
try to set binding this way instead :
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Your current binding means, bind data context to a property named UsersViewModel
declared in it self. That will work if, for example, you have something like this in code behind :
public UsersViewModel UsersViewModel { get; set; }
then DataContext will be set to that property.
UPDATE :
Looking at your answer, you can try do it this way to set DataContext in Page level :
<Page.DataContext>
<local:UsersViewModel />
</Page.DataContext>