我尝试在ScrollViewer中查看项目,但它没有显示任何内容
有Xaml:
<ScrollViewer>
<ItemsControl ItemsSource="{Binding myList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Text}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
确定。我在c#cod中做了一些更改,但它仍然不起作用:
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();
}
}
答案 0 :(得分:1)
有几个原因:
您的observablecollection应该是一个属性。
公共类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" });
}
}
另外请记住,如果您修改“MyItem”,它需要支持INotifyPropertyChanged,否则您的显示将不会更新。
答案 1 :(得分:0)
您应该正确定义ItemTemplate
:
<ScrollViewer>
<ItemsControl ItemsSource="{Binding myList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Text}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>