B.K. B.K. - 1 year ago 155
C# Question

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

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

in WPF.

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
as files get moved around, deleted, renamed, etc. All the list does is keeps tracks of names and paths.

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

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

listboxFolder1.ItemsSource = folder1.Folder;

Here's my XAML portion:

<ListBox x:Name="listboxFolder1" Grid.Row="1" BorderThickness="0"

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

Answer Source

You will need to define the ItemTemplate for your ListBox

    <ListBox x:Name="listboxFolder1" Grid.Row="1" BorderThickness="0" 
           <TextBlock Text="{Binding Name}"/>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download