Lance
Lance

Reputation: 53

Listbox doesn't show the contents of an observablecollection

I have made an application with the mvvm model and a sql database connected to azure. When I try to get information in the database my listbox detects how many objects are in the collection and shows the path to the List instead of the content.

I have tried my sql query and this just gives back the data fine.

view:

            xmlns:local="clr-namespace:Lance_Theunis_r0702301_2ITFA"
            xmlns:viewmodel="clr- 
   namespace:Lance_Theunis_r0702301_2ITFA.ViewModel"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">

        <Window.Resources>
            <viewmodel:WagenViewModel x:Key="WagenViewModel" />
        </Window.Resources>

        <DockPanel LastChildFill="True" DataContext="{StaticResource 
    WagenViewModel}">
            <StackPanel DockPanel.Dock="Left" Width="333px">
                <ListBox ItemsSource="{Binding Path=Wagens}" />
            </StackPanel>

viewmodel:

    namespace Lance_Theunis_r0702301_2ITFA.ViewModel
    {
        class WagenViewModel : BaseViewModel
        {
            public WagenViewModel()
            {
                LeesWagens();
            }
            private ObservableCollection<Wagen> wagens;
            public ObservableCollection<Wagen> Wagens
            {
                get
                {
                    return wagens;
                }
                set
                {
                    wagens = value;
                    NotifyPropertyChanged();
                }
            }
        private void LeesWagens()
        {
            DataService wagenDS = new DataService();

            wagens = new ObservableCollection<Wagen>(wagenDS.GetWagens());
        }
    }
}

DataService class:

namespace Lance_Theunis_r0702301_2ITFA.Model
{
    class DataService
    {
        private static string connectionString =
       ConfigurationManager.ConnectionStrings["azure"].ConnectionString;


        private static IDbConnection db = new SqlConnection(connectionString);

        public List<Wagen> GetWagens()
        {
            string sql = "Select naam from Wagen order by naam";

            return (List<Wagen>)db.Query<Wagen>(sql);
        }

    }
}

There are no error messages. The listbox shows (Lance_Theunis_r0702301_2ITFA.Model.Wagen) instead of for example (bmw m3).

Upvotes: 0

Views: 44

Answers (1)

mm8
mm8

Reputation: 169400

Set the DisplayMemberPath property to "naam" or whatever the name of the property of the Wagen class that you want to display is:

<ListBox ItemsSource="{Binding Path=Wagens}" DisplayMemberPath="naam" />

Upvotes: 2

Related Questions