[英]Dictionary (ObservableCollection) binding to ListView in Windows Phone 8.1
[英]Windows Phone ListView Binding
我試圖創建項目的ListView並能夠刪除它們。 這是我的代碼。 我無法顯示要顯示的項目列表。 我沒有找到綁定ListViews的簡單示例,因此我可以理解確切的概念。 你能告訴我我在做什么錯嗎?
PS。 myListOfItems是帶有字符串的列表。 該項目是WP 8.1 WinRT。
public class MedClass
{
public string medName { get; set; }
}
public class MedClassRoot
{
public List<MedClass> medNames { get; set; }
}
public sealed partial class MedSavedPage : Page
{
protected override void OnNavigatedTo(NavigationEventArgs e)
{
MedClassRoot root = new MedClassRoot();
root.medNames = new List<MedClass>();
foreach (var element in myListOfItems)
{
root.medNames.Add(new MedClass {medName = element});
}
MedSaved_ListView.DataContext = root;
//MedSaved_ListView.ItemsSource = root;
}
<ListView DataContext="{Binding root}"
x:Name="MedSaved_ListView"
HorizontalAlignment="Left"
Height="507"
Margin="10,73,0,0"
VerticalAlignment="Top"
Width="380" Tapped="MedSaved_ListView_Tapped">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Height="30">
<TextBlock Text="{Binding medName}" FontSize="16"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
這是使其生效的最快方法:
現在,從ListView中刪除DataContext部分。
只需將ItemsSource設置為項目列表即可(注釋執行MedSaved_ListView.DataContext = root的部分,然后取消注釋並更改下一行)
MedSaved_ListView.ItemsSource = root.medNames;
說明-ListView ItemsSource是用於生成ItemsControl內容的集合。 您需要將其設置為某種IEnumerable。 您的medNames是實現IEnumerable的List類型,因此您需要為其設置ItemsSource。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.