Welcome to MSDN Blogs Sign in | Join | Help

Mike Hillberg's Blog on Wpf (.Net and Silverlight)

Random, interesting things about Wpf/Silverlight.

Syndication

News

All postings are provided "As Is" with no warranties, and confers no rights. Opinions and views expressed here are not necessarily those of Microsoft Corporation.


Binding to anonymous types in an Xbap or Silverlight application

It's easy to use Linq queries to create objects, and to use {Binding}s to bind properties of those objects into your view.  If you're doing this for an application that will run as an Xbap ("WPF Browser Application") or as a Silverlight app, just note that you need to generate nominal types rather than anonymous types.

For example, say you have a bound ListBox like:

<ListBox Name="ListBox1">

    <ListBox.ItemTemplate>

        <DataTemplate>

            <TextBlock Text="{Binding First}" />

        </DataTemplate>

    </ListBox.ItemTemplate>

</ListBox>


... where the ListBox ItemsSource is established with the following code at initialization-time:

public partial class Window1 : Window

{

    private Collection<Person> _collection = new Collection<Person>()

    {

        new Person() { FirstName = "John", LastName = "Doe" },

        new Person() { FirstName = "Jane", LastName = "Doe" },

        new Person() { FirstName = "Fred", LastName = "Flinstone" }

    };

 

    public Window1()

    {

        InitializeComponent();

 

        ListBox1.ItemsSource = from person in _collection

                               where person.LastName == "Doe"

                               select new { First = person.FirstName, Last = person.LastName };

 

    }

 

}


This won’t show anything in the ListBox.  (You might see debug output like “
System.Windows.Data Error: 12 : Cannot get 'First' value (type 'String') from '' (type '<>f__AnonymousType0`2')”).

The issue here is that the select statement in the query (select new { First = person.FirstName, Last = person.LastName }) generates an internal (anonymous) type, and with an Xbap or Silverlight app you can’t bind to internal types.  The solution is to use a nominal type.  E.g., add this class definition:

public class SelectedPerson

{

    public string First { get; set; }

    public string Last { get; set; }

}


... and use it in the select:

ListBox1.ItemsSource = from person in _collection

                       where person.LastName == "Doe"

                       select new SelectedPerson { First = person.FirstName, Last = person.LastName };


... and your list box is populated with “John” and “Jane”.

Published Monday, May 05, 2008 9:56 AM by MikeHillberg

Comments

# Dew Drop - May 6, 2008 | Alvin Ashcraft's Morning Dew @ Tuesday, May 06, 2008 9:05 AM

PingBack from http://www.alvinashcraft.com/2008/05/06/dew-drop-may-6-2008/

Dew Drop - May 6, 2008 | Alvin Ashcraft's Morning Dew

# WPF/Silverlight/XAML Web News - 2008/05/23 @ Friday, May 23, 2008 4:29 PM

My latest links about WPF (Apps, Controls, 3.5sp1 beta, HowTo, for LOB), Silverlight, XAML and URLs WPF

Rob Relyea - Xamlified

Anonymous comments are disabled
Page view tracker