Free!  FoxPro to WPF screen converter
   Almost free!  One day of FREE VFP to WPF evaluation of your FoxPro app ONSITE
   Free!  Read the foreword to Les' new book (available here)...
Skip Navigation Links

Populating a WPF ListBox with IdeaBlade Devforce

Two minutes may change the way you build software

Les Pinter

Login or register to download source code

Coding time: 2 minutes.    Watch the video

Jump to:   Install DevForce   Creating the application   The code   Conclusion 



In this article, you'll learn how to build a WPF form containing a listbox that lists a column from a SQL table. In the process, you'll see how incredibly simple it is to build high-quality, useful applications in C# or VB in a fraction of the time you might have thought it would take.

The secret is the IdeaBlade framework - a collection of controls, components and methodologies, bound together by Business Objects - classes generated by the DevForce Business Object Mapper - that contain the code necessary to retrieve and save their associated data from SQL Server, Oracle, FoxPro DBFs, and just about any other data store.

Installing DevForce

DevForce works with just about every possible data source, but the easiest one to work with is SQL Server. If you installed Visual Studio 2008, a run-time version called SQLExpress was installed. The DevForce installer attaches a test database called NorthWindIB, so you should have SQL installed before you install DevForce.

If you haven't already done so, you'll need to install DevForce. Go to and request an evaluation copy, which is fully functional but limits the size of your models to 10 tables.

The installation process will add a DevForce Object Mapper menu bar under the Tools menu pad of your Visual Studio 2008 IDE. The DevForce Object Mapper is used to generate code that makes communicating with your data as easy as you hoped it would be before you saw .NET. More on that later.

Creating a WPF Application

From the IDE menu, select File, New, Project, then select either VB or C#, and from the Windows templates group select WPF Application, supplying the project name WPFListBoxPopulation (Fig. 1). After a few seconds you'll have a project that looks like Fig. 2 in the Solution Explorer. The generated Window contains a grid, which is one of five containers you can use. Note that this isn't a grid as in DataGridView; it's just a container. Inside it, add a ListBox tag, which doesn't need any attributes just yet (Fig. 3):

Adding the Entity Data Model

The next step is to add an Entity-Data model. In the Solution Explorer, right-click on the project name and select Add, New Item from the context menu (Fig. 4). Select ADO.NET Entity Data Model; the default name (Model1.edmx) will do for now (Fig. 5). Select "Generate from Database"; this will allow you to point to the database and table(s) you want to use. DevForce's Object Mapper piggy-backs on this model (Fig. 6). I had previously created a Data Connection to the NorthWindIB database, but on your screen the combobox will be empty, so click on New Connection (Fig. 7). Once the list of available SQL Servers populates, select the one where you installed the NorthWindIB database (Fig. 8), and select the NorthWindIB database from the database combobox (Fig. 9).

Since the Entity Data Model Wizard doesn't know where you just came from, it asks if you want to use the connection string for the connection that you just created. This string will be saved in App.Config (Fig. 10). Next, you'll see a list of tables, views and stored procedures in the NorthWindIB database. Expand the Tables node and check the Customer table to select it (Fig. 11). After the Entity Data Model is generated, the designer opens and shows you a graphical view of the model. It's just one table in our simple case, but it could contain as many tables, views and stored procedures as you wish (Fig. 12).

Generating the business object(s)

We're ready to use the DevForce Object Mapper to generate the Business Object. From the menu, select Tools, DevForce Object Mapper (Fig. 13). Click on the New Model node (Fig. 14); the defaults that are supplied are fine for this walk-through (Fig. 15). From the menu, select Model, Add Entity Model (Fig. 16)..., and then select Model1.edmx, the only model file in our project (Fig. 17). In the case of this little model, the nodes of the model tree are populated instantly (Fig. 18). You can drill down through the tree to see what's about to be generated (Fig. 19). (In subsequent walk-throughs you'll see how to use the available options, which are way cool; for now, just ignore them.) Select File, Save from the menu to generate the business object code, then close the Object Mapper dialog (Fig. 20).

In case you wonder what just happened, a class in a namespace called DomainModel was generated in a file with an .ibedmx extension (Fig. 21). In our case, it has the same name as the project. However, the DomainModel namespace is what we'll use to find the Customer class that was generated:

The code

There are only four lines of code in this example, plus a small change that we'll need to make to the XAML underlying Window1.

  • First, we'll create an object called Manager, based on the DomainModelEntityManager in the DomainModel namespace in our generated code:
  • Next, we'll call the Customer method of this Manager, and convert the output to a list called customers;
  • Next, we'll select the CompanyName column from the customers list using LINQ and store the values in another list called names;
  • Finally, we'll assign this list to the windows's DataContext.

The only thing left to do is to assign the names list collection to the ListBox's DataContext: Change line 6 of your window1.xaml file to this (Fig. 22):

    <ListBox ItemsSource="{Binding}" />

Press F5 to see the application run (Fig. 22):


There are five steps in this exercise:

  1. Creating the WPF project;
  2. Creating the EF model;
  3. Generating the DevForce model;
  4. Writing a few lines of code to retrieve the data; and
  5. Adding the XAML for your UI and setting the DataContext and (if relevant) path(s).

All we really needed was four lines of code and one little sliver of XML. Once you've memorized the steps in this process and don't need to read this guide to recall each step, it goes really fast. Repeat this exercise until you can reproduce it without reference to this walk-through, and you'll find that it's about two minutes - if that.

I hope you had your stopwatch on, and I hope that you're pleasantly surprised. Compare this to the amount of code you'd write to do this by hand, then consider that you've only seen the tip of the iceberg. This is what .NET was supposed to be like.

On my website, I make an offer that I hope you'll take seriously. Download IdeaBlade DevForce and use it to build your next application. If you get stuck, email me at, and I'll walk you through it. I fully expect to convince you that, if you're writing WinForms applications, you need IdeaBlade DevForce. It's an offer you can't refuse.

See 'ya.


Copyright(C) Pinter Consulting, 2012Tel: +1 (650) 464-6924
Automated conversion between C# and VB by Visible C#/Visible VB from Tangible Software Solutions