Click here to Skip to main content
13,198,563 members (51,998 online)
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 11 Aug 2014

Basic MVVM Listbox Binding in WPF

, 11 Aug 2014
Rate this:
Please Sign up or sign in to vote.
Basic MVVM pattern for binding items to a listbox in WPF


Basic MVVM pattern for binding items to a listbox in WPF, this pattern can also be used for Winforms just how you set the control will be done in code behind.


This example shows how to bind items from a directory to a listbox. You can use the same methodology to bind any collection to the listbox. This is my first tip, so I am not sure how to do the formatting correctly. This same approach can be used for the combobox.

Using the Code

This is a very basic overview of how to do binding to a listbox. I have included the entire pattern I use to achieve my result.

// The ViewModelBase
public abstract class ViewModelBase : INotifyPropertyChanged
        #region Property Changed Event Handler
        public void SetPropertyChanged(string propertyName)
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion Property Changed Event Handler
// Basic Model

public class FileObject : ViewModelBase
        #region Properties
        #region Location
        private string location = string.Empty;
        public string Location
            get { return location; }
                if (value != this.location)
                    location = value;
        #endregion Location

        #region FileName
        private string fileName = string.Empty;
        public string FileName
            get { return fileName; }
                if (value != this.fileName)
                    fileName = value;
        #endregion FileName
        #endregion Properties

        #region String Override
        public override string ToString()
            //.FormatString(this string myString) is an extension.
            string returnString = string.Empty;
            if (this.fileName != string.Empty)
                returnString = String.Format("File Name: {0}.",this.fileName);
            return returnString;
        #endregion String Override
// View Model

public class ViewModel : ViewModelBase
        #region Constructor
        public ViewModel()
        #endregion Constructor

        #region Properties
        #region SelectedFileObject
        private FileObject selectedFileObjects;
        public FileObject SelectedFileObject
            get { return selectedFileObjects; }
                if (value != this.selectedFileObjects)
                    selectedFileObjects = value;
        #endregion SelectedFileObject

        #region FileObjectCollection
        private ObservableCollection<FileObject> fileObjectCollection;
        public ObservableCollection<FileObject> FileObjectCollection
            get { return fileObjectCollection; }
                if (value != this.fileObjectCollection)
                    fileObjectCollection = value;
        #endregion FileObjectCollection

        #region Path
        //Use Your own path
        private string path = @"C:\Users\pcName\Downloads\Gifs";
        public string Path
            get { return path; }
                if (value != this.path)
                    path = value;
        #endregion Path
        #endregion Properties

        #region Mehods
        #region void setFiles()
        private void setFiles()
            if (this.path != string.Empty)
                DirectoryInfo dInfo = new DirectoryInfo(this.path);
                //use any extension you want.
                FileInfo[] fInfo = dInfo.GetFiles("*.gif");
        #endregion void setFiles()

        #region Action<FileInfo> setFileObjectCollection()
        private Action<FileInfo> setFileObjectCollection()
            this.fileObjectCollection = new ObservableCollection<FileObject>();
            return f => this.fileObjectCollection.Add(new FileObject 
			{ FileName = f.Name, Location = f.DirectoryName });
        #endregion Action<FileInfo> setFileObjectCollection()
        #endregion Mehods
// View

    Title="GifViewer" Height="350" Width="525">
        <ListBox ItemsSource="{Binding FileObjectCollection}" 
		SelectedItem="{Binding SelectedFileObject}"/>


  • 2014/08/12: First draft


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
South Africa South Africa
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionIt does not work at all Pin
Member 113031399-Jul-15 20:39
memberMember 113031399-Jul-15 20:39 
Bug"this.SetPropertyChanged("FileName")" cannot be used due to protection Pin
CoderMan127-May-15 5:26
memberCoderMan127-May-15 5:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171020.1 | Last Updated 12 Aug 2014
Article Copyright 2014 by Jdarmanovich
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid