[英]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.