Home > WPF > CodeFluent Entities and WPF: Selecting Data

CodeFluent Entities and WPF: Selecting Data

Following our ASP.NET post series, this post starts a new one but this time in WPF Smile

Selecting data in a WPF application using CodeFluent Entities generated .NET classes, known as the Business Object Model (BOM), is very straightforward. There’s actually very little to do since the generated classes implement all .NET interfaces so developers get the best experience possible when building there UI: IEnumerable, IEnumerable<T>, IList, IList<T>,  ICollection, ICollection<T>, IBindingList, INotifyPropertyChanged, INotifyCollectionChanged, IRaiseItemChangedEvents, etc.

Therefore, to display a list of items in an ItemsControl all you have to do is is set the loaded collection as the ItemsSource of the control:


    <StackPanel Orientation="Horizontal" Margin="0,5,0,5">
        <Button Name="_loadAllButton" Content="LoadAll" Click="OnLoadAllButtonClick" Width="100" Margin="20,5,20,5" />
        <Button Name="_clearAll" Content="Clear" Click="OnClearAllClick" Width="100" Margin="20,5,20,5" />
    <ListBox Name="_contactsListBox" />


private void OnLoadAllButtonClick(object sender, RoutedEventArgs e)
    _contactsListBox.ItemsSource = ContactCollection.LoadAll();

private void OnClearAllClick(object sender, RoutedEventArgs e)
    _contactsListBox.ItemsSource = null;

This will display the following:


As you can see, if no data template is specified, the Entity Display Name is used which in our case is the FirstName property of our Contact entity.

If you want to display your contacts in the following format: “<LastName>, <FirstName>” you can define a data template such as this:

<ListBox Name="_contactsListBox">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding LastName}" FontWeight="Bold" />
                <TextBlock Text=", " />
                <TextBlock Text="{Binding FirstName}" />

Here’s the result:


  1. No comments yet.
  1. April 16, 2012 at 10:28 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s