WPF/C# 将自定义对象列表数据绑定到 ListBox?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18692738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:56:32  来源:igfitidea点击:

WPF/C# Binding custom object list data to a ListBox?

c#wpflistlistboxlistboxitems

提问by B.K.

I've ran into a bit of a wall with being able to bind data of my custom object list to a ListBoxin WPF.

我遇到了一些障碍,无法将自定义对象列表的数据绑定到ListBoxWPF 中的 a。

This is the custom object:

这是自定义对象:

public class FileItem
{
    public string Name { get; set; }
    public string Path { get; set; }
}

And this is the list:

这是清单:

private List<FileItem> folder = new List<FileItem>();
public List<FileItem> Folder { get { return folder; } }

The list gets populated and maintained by a FileSystemWatcheras files get moved around, deleted, renamed, etc. All the list does is keeps tracks of names and paths.

FileSystemWatcher随着文件的移动、删除、重命名等,该列表由 a 填充和维护。该列表所做的只是跟踪名称和路径。

Here's what I have in the MainWindow code-behind file (it's hard coded for testing purposes for now):

这是我在 MainWindow 代码隐藏文件中的内容(目前已硬编码用于测试目的):

FolderWatcher folder1 = new FolderWatcher();
folder1.Run(@"E:\MyApp\test", "*.txt");

listboxFolder1.ItemsSource = folder1.Folder;

Here's my XAML portion:

这是我的 XAML 部分:

<ListBox x:Name="listboxFolder1" Grid.Row="1" BorderThickness="0" 
         ItemsSource="{Binding}"/>

Unfortunately, the only thing that gets displayed is MyApp.FileItemfor every entry. How do I display the specific property such as name?

不幸的是,唯一显示的是MyApp.FileItem每个条目。如何显示名称等特定属性?

采纳答案by Nitin

You will need to define the ItemTemplate for your ListBox

您需要为 ListBox 定义 ItemTemplate

    <ListBox x:Name="listboxFolder1" Grid.Row="1" BorderThickness="0" 
     ItemsSource="{Binding}">
       <ListBox.ItemTemplate>
         <DataTemplate>
           <TextBlock Text="{Binding Name}"/>
         </DataTemplate>
       </ListBox.ItemTemplate>
     </ListBox>

回答by Sayse

The easiest way is to override ToStringon your FileItem, (The listbox uses this to populate each entry)

最简单的方法是覆盖ToString您的FileItem, (列表框使用它来填充每个条目)

    public override string ToString()
    {
        return Name;
    }

回答by Omri Btian

Each item in the list that ListBox shows automatically calls the ToStringmethod to display it, and since you didn't override it, it displays the name of the type.

ListBox 显示的列表中的每个项目都会自动调用该ToString方法来显示它,并且由于您没有覆盖它,所以它会显示类型的名称。

So, there are two things you can do here.

因此,您可以在这里做两件事。

  1. Override the ToStringmethod like Sayse suggested.
  2. Use DataTemplateand bind each of your properties seperatly
  1. 覆盖ToStringSayse 建议的方法。
  2. 使用DataTemplate单独绑定每个属性

In your resource add the template with a key

在您的资源中添加带有键的模板

        <DataTemplate x:Key="fileItemTemplate">
            <StackPanel>
                <TextBlock Text="{Binding Name}"/>
                <TextBlock Text="{Binding Path}"/>
            </StackPanel>
        </DataTemplate>

and give it as your listbox ItemTemplate

并将其作为您的列表框 ItemTemplate

<ListBox x:Name="listboxFolder1" Grid.Row="1" BorderThickness="0"  ItemsSource="{Binding}" ItemTemplate="{StaticResource fileItemTemplate}">

回答by amb9800

In case anyone comes across this now via search, I just encountered pretty much the same issue in a C# UWP app.

如果现在有人通过搜索遇到这个问题,我只是在 C# UWP 应用程序中遇到了几乎相同的问题。

While the XAML bits in Nitin's answer above were necessary, they didn't fix the issue alone -- I also had to change my equivalent of Folderto be an ObservableCollection, rather than a List, to get the ListBox to show the property I needed.

虽然上面 Nitin 的答案中的 XAML 位是必要的,但它们并不能单独解决问题——我还必须将我的等效项Folder更改为 anObservableCollection而不是 a List,以使 ListBox 显示我需要的属性。