Reputation: 283
I try to view items in ScrollViewer but it display nothing
There is Xaml:
<ScrollViewer>
<ItemsControl ItemsSource="{Binding myList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Text}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
Ok. I make some changes in c# cod but it still does not work:
public class MyItem
{
string text;
public string Text
{
set { text = value; }
get { return text; }
}
}
public partial class MainPage : PhoneApplicationPage
{
public ObservableCollection<MyItem> myList { get; set; }
public MainPage()
{
myList = new ObservableCollection<MyItem>();
myList.Add(new MyItem() { Text = "Abkhazia" });
myList.Add(new MyItem() { Text = "Afghanistan" });
myList.Add(new MyItem() { Text = "Albania" });
InitializeComponent();
}
}
Upvotes: 0
Views: 1112
Reputation: 7183
A few reasons:
Your observablecollection should be a property.
public class MyClass {
public ObservableCollection<MyItem> myList {get; set;}
public MyClass()
{
DataContext=this;
myList = new ObservableCollection();
myList.Add(new MyItem() { Text = "Abkhazia" });
myList.Add(new MyItem() { Text = "Afghanistan" });
myList.Add(new MyItem() { Text = "Albania" });
}
}
Also keep in mind if your modifying "MyItem" it needs to support INotifyPropertyChanged or your display will NOT update.
Upvotes: 1
Reputation: 34407
You should define ItemTemplate
properly:
<ScrollViewer>
<ItemsControl ItemsSource="{Binding myList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Text}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
Upvotes: 0