c# - 您好,我想解压缩评论列表 (ICollection) 并使用 c# 中的数据绑定显示评论。如何访问列表中的每条评论?
问题描述
<StackPanel Grid.Column="1">
<ListView x:Name="lstvActiveIssues" ItemClick="lstvActiveIssues_ItemClick" IsItemClickEnabled="True" Height="500">
<ListView.ItemTemplate>
<DataTemplate x:DataType="models:Issue">
<StackPanel>
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Title:" Width="130" FontSize="10"/>
<TextBlock Text="{x:Bind Title}" Margin="10 10 0 0" />
</StackPanel>
</StackPanel>
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Customer Name:" Width="130" />
<TextBlock Text="{x:Bind Customer.FirstName}" Margin="10 5 0 0" />
<TextBlock Text="{x:Bind Customer.LastName}" Margin="5 5 0 0" />
</StackPanel>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<StackPanel DataContext="{Binding SelectedItem, ElementName=lstvActiveIssues}" >
<TextBlock Text="Details of the selected Issue" />
<StackPanel Orientation="Horizontal">
<TextBlock Text="Title:" Width="130" />
<TextBlock Text="{Binding Title}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Customer Name:" Width="130" />
<TextBlock Text="{Binding Customer.FirstName}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Comment:" Width="130" />
<TextBlock Text="{Binding Comments}" />
</StackPanel>
</StackPanel>
</StackPanel>
问题类在这里
public class Issue
{
public Issue()
{
}
public Issue(long id, long customerId, string title, string description, string status, DateTime created)
{
Id = id;
CustomerId = customerId;
Title = title;
Description = description;
Status = status;
Created = created;
}
public long Id { get; set; }
public long CustomerId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Status { get; set; }
public DateTime Created { get; set; }
public virtual Customer Customer { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
textblock 仅显示 System.Collection.Generic.List'1[DataAccessLibrary.Models.Comment] 如何显示文本块中的每条评论。我想解压缩评论列表(ICollection)并使用 c# 中的数据绑定显示评论。如何访问列表中的每条评论?
解决方案
我认为您的问题是您将 ICollection 对象直接绑定到文本块的文本属性。
如果您只想直接显示文本块中的值,您可以使用值转换器将您的评论列表转换为将显示在您的文本块中的字符串。但是,根据您的问题,我认为这不是您想要的结果。
我的建议是使用另一个列表视图来显示评论而不是文本块。所以像这样的东西可能是你正在寻找的东西:
<ListView x:Name="issueCommentsList" ItemSource="{Binding SelectedItem.Comments, ElementName=lstvActiveIssues}" IsItemClickEnabled="True" Height="500">
<ListView.ItemTemplate>
<DataTemplate x:DataType="models:Comment">
<StackPanel>
<TextBlock Text="{Binding Text}" FontSize="10"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
如您所见,列表视图的 ItemSource 将数据绑定到 Selected Items 注释。这将允许您为您的评论创建自定义 UI。
我刚刚为评论 UI 添加了一个简单的文本块,因此您可以相应地更改它
希望这会有所帮助,快乐编码:)
推荐阅读
- javascript - AngularJs范围参数未定义
- redux - Redux:更新列表中的一项而不更新依赖于该列表的所有其他组件
- visual-studio-code - 如何调试 Visual Studio Code 扩展?
- python - 在 Mac 上的 pycharm 中安装 Scikit-learn
- javascript - 将服务器推送通知解析到用户数组
- vba - 检查范围是否适合单行
- ios - 如何向下平移以关闭键盘?
- python - SQLAlchemy:关系字段值
- python - iPython 调试器引发 `NameError: name ... is not defined`
- python - 用于 3D 三角形网格的 Matplotlib“补丁”功能