首页 > 解决方案 > 您好,我想解压缩评论列表 (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# 中的数据绑定显示评论。如何访问列表中的每条评论?

标签: c#listuwp

解决方案


我认为您的问题是您将 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 添加了一个简单的文本块,因此您可以相应地更改它

希望这会有所帮助,快乐编码:)


推荐阅读