在 WPF 中绑定 DTO - 我只想显示文件名。当前它正在显示"DTO"



我已将列表框绑定到我的 BindableCollection,但我只想显示文件名。目前它显示"DTO"

public class FilesDTO : IDTO
{
    public int Id { get; set; }
    public string FileName { get; set; }
    public string FileExtension { get; set; }
    public byte[] FileArray { get; set; }
}
using (var ctx = DB.Get())
    {
         Files = new BindableCollection<FilesDTO>(ctx.Files.Select(x => new FilesDTO { FileArray = x.  FileArray, FileExtension = x.FileExtension, FileName = x.FileName, Id = x.Id }));
    }

Xaml方面:

<ListBox x:Name="Attachments" Grid.Row="2" ItemsSource="{Binding Files}"  />

我试过{绑定文件.文件名}也不起作用。我的猜测是创建绑定到:S

的属性
您需要

设置ItemsTemplate以正确显示您的成员。 例如,以下内容将使用简单的TextBlock显示文件列表:

<ListBox x:Name="Attachments" Grid.Row="2" ItemsSource="{Binding Files}">
   <ListBox.ItemTemplate>
     <DataTemplate>
         <TextBlock Text="{Binding Path=FileName}" />
     </DataTemplate>
   </ListBox.ItemTemplate>
 </ListBox>

设置 DisplayMemberPath:

<ListBox DisplayMemberPath="FileName" />

最新更新