[英]ListBox in wp7 doesn't work for me
我正在嘗試在wp7中呈現一個列表視圖,由於某種原因,它似乎不起作用
我的xaml
<!--ContentPanel - place additional content here-->
<StackPanel x:Name="ContentPanel2" Grid.Row="1" Margin="12,0,12,0">
<ListBox x:Name="list">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="5">
<Image Source="{Binding ImageUri}" Stretch="None"/>
<TextBlock Text="{Binding Text}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
我的C#代碼
public class list
{
public string title { get; set; }
public string imageSource { get; set; }
}
和
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
List<list> dataSources = new List<list>();
dataSources.Add(new list() { title = "Shacharit", imageSource = "Images/shacharit.png" });
dataSources.Add(new list() { title = "Mincha", imageSource = "Images/mincha.png" });
dataSources.Add(new list() { title = "Arvit", imageSource = "Images/arvit.png" });
dataSources.Add(new list() { title = "Birkat HaMazon", imageSource = "Images/shacharit.png" });
list.ItemsSource = dataSources;
}
提前致謝
請嘗試以下操作,將image和text塊的綁定更改為綁定到您當前聲明的字符串,您正在嘗試綁定到ImageURI和Text,並且它們在您的任何代碼中均不存在。
<!--ContentPanel - place additional content here-->
<StackPanel x:Name="ContentPanel2" Grid.Row="1" Margin="12,0,12,0">
<ListBox x:Name="list" Da>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="5">
<Image Source="{Binding imageSource }" Stretch="None"/>
<TextBlock Text="{Binding title}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
為了弄清喬恩·D的答案,您正在后面的代碼中創建具有“ imagePath”和“ title”屬性的數據對象
new list() { title = "Shacharit", imageSource = "Images/shacharit.png" };
但嘗試使用“ ImageUri”和“ Text”屬性。
在VS的輸出窗口中,您應該看到這些綁定錯誤出現。
以下兩行(您在XAML中綁定的位置)應該可以為您解決問題……
<Image Source="{Binding imageSource }" Stretch="None"/>
<TextBlock Text="{Binding title}"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.