Reputation: 51
I'm new to windows phone 8 development and I can't find a way to add a listview control and populate it with items.
Could someone help me a lil.bit?
Thanks
Upvotes: 0
Views: 492
Reputation: 415
ListBox structure is similiar to ListView.You just try this.If you don't know MVVM architecture, try to learn the basics of that.
<ListBox ItemTemplate="{StaticResource MasterTemplate}"
ItemsSource="{Binding ItemLists}"
HorizontalAlignment="Stretch">
<ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Name,Mode=TwoWay}"/>
</DataTemplate>
</ItemTemplate>
</ListBox>
The above shows the view.In the viewmodel,you can just declare a observablecollection like this.
public ObservableCollection<ProductModel> ItemLists{get;set;}
Then initialize the list like this,
ItemLists=new ObservableCollection<ProductModel>();
Then add elements to list and it will automatically display in the view because of binding.
private void addProducts()
{
for(int i=0;i<5;i++)
{
ItemLists.Add(new ProductModel{Name="Product"});
}
}
This is the simple example.In the productmodel,you just declare the name property.
private string name;
public string Name
{
get{return name;}
set
{
name=value;
OnPropertyChanged("Name");
}
}
Upvotes: 2
Reputation: 5557
You can make use of ListBox control in windows phone
A Simple ListBox looks like this(though it can be much more complex with DataBinding etc).
<ListBox >
<ListBoxItem Content="Item 1" />
<ListBoxItem Content="Item 2" />
<ListBoxItem Content="Item 3" />
<ListBoxItem Content="Item 4" />
</ListBox>
Upvotes: 0