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

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


You will need to define the ItemTemplate for your ListBox

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


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

    public override string ToString()    {        return Name;    }


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

So, there are two things you can do here.

  1. Override the ToString method like Sayse suggested.
  2. Use DataTemplate and bind each of your properties seperatly

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

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