我想知道你们有些人是什么类型的,但以下对我有用:
请注意,我已经稍微重命名了一些内容,并将所有内容放在相同的名称空间中。
var comments = new Comments();
var threeComments = new List<Comment>
{
new Comment("t1", "d1", "n1", "i1"),
new Comment("t2", "d2", "n2", "i3"),
new Comment("t3", "d3", "n3", "i3")
};
comments.comments = threeComments.ToArray();
commentsLooper.ItemsSource = new CommentsDataItems(comments);
public class Comments
{
public Comment[] comments { get; set; }
}
public class CommentsDataItems : ObservableCollection<CommentDataItem>
{
public CommentsDataItems(Comments comments)
{
foreach (var com in comments.comments)
{
Add(new CommentDataItem(com.text, com.device, com.name, com.id));
}
}
}
public class CommentDataItem
{
public String text { get; set; }
public String device { get; set; }
public String name { get; set; }
public String id { get; set; }
public CommentDataItem(String text, String device, String name, String id)
{
this.text = text;
this.device = device;
this.name = name;
this.id = id;
}
}
public class Comment
{
public String text { get; set; }
public String device { get; set; }
public String name { get; set; }
public String id { get; set; }
public Comment(String text, String device, String name, String id)
{
this.text = text;
this.device = device;
this.name = name;
this.id = id;
}
}
在XAML中,我做了一些小改动。其中最大的一个就是去掉了明显的高度和宽度。值得注意的是,因为你在网格上设置了一个小的相关高度,所以只有一个空间可以看到一个项目。
<Grid x:Name="commentsCotainer" VerticalAlignment="Top">
<ItemsControl x:Name="commentsLooper" Grid.Row="1">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid x:Name="comentHolder" HorizontalAlignment="Center" VerticalAlignment="Top">
<TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Top" TextWrapping="Wrap" Text="{Binding text}" Foreground="White" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>