Reputation: 1089
Iam trying to bring the values of static variable from the following class
public class LoginUserId
{
public static int id { get; set; }
public static string username { get; set; }
}
from here i created another class to get values as following
public class UsersList
{
public static List<LoginUserId> Names { get; set; }
}
Now i have xaml page as
<ListView ItemSelected="LoginUserList_OnItemSelected" ItemsSource="{Binding Source={x:Static lacal:UsersList.Names}}" IsVisible="True" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding username}" TextColor="Black" FontSize="20" IsVisible="True"></Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
At output iam not getting anything just a blank page is appearing,please correct me where i went wrong.
Upvotes: 0
Views: 1525
Reputation: 14475
modify your code as below
public class LoginUserId
{
public int id { get; set; }
public string username { get; set; }
}
public class UsersList
{
public static List<LoginUserId> Names = new List<LoginUserId>() {
new LoginUserId{id = 1, username = "No1"},
new LoginUserId{id = 2, username = "No2"},
};
}
The List Names must be static , but the property of object in it should not be static , because we must initalize the variable for the model and assign the value for the properties, it's not steady.
Upvotes: 0
Reputation: 215
You can't bind to static variables with the way you did it in your XAML. When you use Text="{Binding username}"
your property has to be a non static Property public string username { get; set; }
if you want to bind to your static property then you have to use the x:Static
way.
<Label Text="{x:Static local:YourViewModel.username}" />
of course you have to add a namespace of your viewModels to the ContentPage
Upvotes: 1