C# 3.0 and WPF’s ListView

In many applications you want to fill up a ListView or a GridView with some data. In WPF applications you set the View-Property of the ListView to a GridView to show the data nicely in columns. In most cases you have an entity class (think of it as a "real" entity class or a DataRow as part of a DataTable), and objects of this class should be represented in a row of the GridView. In WPF the DisplayMemberBinding-Property of each GridViewColumn is bound to a property of the entity class, so objects of the entity class can be easily added to the ListView and are displayed correctly in the GridViewColumns. But…

The problem (… the problem is more code as you want to have) begins, when you have no entity class and your data is stored in objects of different classes, maybe only in some variables, and you just want to display the data in one row of the ListView. If your variables have a relation to each other, it’s semantically correct to put them into one row of the ListView, displayed in different GridViewColumns. But to display the data in the ListView, you have to create a dummy entity class that contains your data. There’s no other way!

… no other way with C# 2.0. But now we have C# 3.0, and things can get much easier, cause the compiler can create nearly everything for you. :-) To show you how some C# 3.0 features can help you for the problem above with the dummy entity class, I have to step some months back in time and pick out a small sample that exactly shows the necessity of creating a dummy entity class.

Some months ago I created a small WPF application (.NET 3.0 and C# 2.0) to show developers how Routed Events are routed through the Element Tree. The application contains a Button and a ListView. The Button contains a StackPanel and inside this StackPanel is a Rectangle. Below the Window1.xaml:

<Window x:Class="RoutedEventsBeispiel.Window1"
Title="RoutedEventsBeispiel" Height="450" Width="500">
   <RowDefinition Height="Auto"/>
  <Button x:Name="button1" Width="100" 
          Height="100" Margin="5">
   <StackPanel x:Name="stackPanel1" Background="Black">
    <Rectangle x:Name="rectangle1" Margin="10"
                 Fill="Red" Width="50" Height="50"/>
  <ListView x:Name="listView" Grid.Row="1">
     <GridViewColumn Header="RoutedEvent"
       DisplayMemberBinding="{Binding RoutedEventName}"/>
     <GridViewColumn Header="Sender"
       DisplayMemberBinding="{Binding Sender}"/>

Especially take a look at the DisplayMemberBinding-Property of the two GridViewColumns in the Window1.xaml above. To represent some data in this ListView, the Data must be represented by an object that has a Property RoutedEventName and a Property Sender. Therefore the Dummy class joins the game. Of course the class could be named in a better way like "RoutedEventInfo", but I decided to name it Dummy, because later with C# 3.0 this class won’t be necessary anymore:

public class Dummy
  private string _sender;
  private string _routedEventName;

  public string Sender
    get { return _sender; }
    set { _sender = value; }
  public string RoutedEventName
    get { return _routedEventName; }
    set { _routedEventName = value; }

In the constructor of Window1 in the codebehind Window1.xaml.cs a general Eventhandler for the Events UIElement.PreviewMouseLeftButtonDown and UIElement.MouseLeftButtonDown is added to the Window, the Button, the StackPanel and the Rectangle. The third parameter of the AddHandler-Method is always set to true. This true stands for "handledEventsToo", so the GeneralOnMouseDown-Eventhandler is called even if any EventHandler has set the Handled-Property of the RoutedEventArgs to true (That’s exactly what the ButtonBase class does internally with the MouseLeftButtonDown-Event, when the ButtonBase class raises its own Click-Event. So the third parameter is necessary here to get the application show the tunneling and bubbling of the events like expected)

public partial class Window1 : System.Windows.Window
  public Window1()


    new RoutedEventHandler(GeneralOnMouseDown), true);
    new RoutedEventHandler(GeneralOnMouseDown), true);
    new RoutedEventHandler(GeneralOnMouseDown), true);
    new RoutedEventHandler(GeneralOnMouseDown), true);
    new RoutedEventHandler(GeneralOnMouseDown), true);
    new RoutedEventHandler(GeneralOnMouseDown), true);
    new RoutedEventHandler(GeneralOnMouseDown), true);
    new RoutedEventHandler(GeneralOnMouseDown), true);
void GeneralOnMouseDown(object sender,RoutedEventArgs e)
   if (sender is Window1 

    && e.RoutedEvent==UIElement.PreviewMouseLeftButton...)
   Dummy d = new Dummy();
   d.Sender = sender.GetType().Name;
   d.RoutedEventName = e.RoutedEvent.Name;

The general Eventhandler GeneralOnMouseDown clears the Items of the ListView if the sender is a Window1 instance and the routed event is the tunneling event PreviewMouseLeftButtonDown. After that, it creates an instance of type Dummy, sets its Sender- and RoutedEventName-Property, and adds the Dummy instance to the ListView.

When you click on the red Rectangle located inside of the black StackPanel that is located inside of the Button, the ListView is filled up with Dummy objects, and it shows us, in which order the routed events are raised. The PreviewMouseLeftButtonDown-Events tunnels down from the Window1 to the Rectangle, then the MouseLeftButtonDown-Event bubbles up to the Window1.


That’s the application like I developed it with C# 2.0 and .NET 3.0. The need of the Dummy class is only necessary, because I got two string variables (the name of the routed event and the type name of the sender) that I have to put into one object, and this one object is put into the ListView. The GridViewColumns are bound to the Properties Sender and RoutedEventName of the object inside the ListView, so the Dummy objects are displayed like expected in the correct GridViewColumns.

Now how can I shorten my code with C# 3.0? We just focus on the GeneralOnMouseDown-Eventhandler and the Dummy-class. To add a Dummy object to the ListView, you can just use the Object Initializer to get it done in one instead of four statements:

listView.Items.Add(new Dummy() { 
    Sender = sender.GetType().Name,
    RoutedEventName = e.RoutedEvent.Name });

Of course you could alternatively add a constructor to the Dummy-class instead of using an object initializer and you would reach a similar effect. But not always you’re the owner of such a Dummy-class and so you won’t be able to add a constructor. Even if you’re the owner of the Dummy-class, I think you like it to write less code (if you’re not paid per lines of code :-) ). With object initializers you won’t have to create many different constructors.

Now take a look at the Dummy class itself. It could be much shorter, if you use the Automation Properties of C# 3.0 (the IL-Code will still look the similar like before). With automation properties, the compiler creates the backing private fields for the specified properties:

public class Dummy
  public string Sender { get; set; }
  public string RoutedEventName{get;set;}

We can go even further.

If you’ve read the C# 3.0 specification, you’ve also read about Anonymous Types. So let’s go ahead one more step. Anonymous types are not only a feature that is good to use in LINQ-statements. In this small application here we could throw the Dummy class away and instead of Dummy objects we can add anonymous type objects to the ListView. The anonymous type must contain the properties Sender and RoutedEventName. So without the Dummy class you could fill up the ListView in the GeneralOnMouseDown-Eventhandler just with anonymous types like below, and the application still works the same way:

void GeneralOnMouseDown(object sender,RoutedEventArgs e)
  if (sender is Window1 && e.RoutedEvent==...)

  listView.Items.Add(new { Sender = sender.GetType().Name,
    RoutedEventName = e.RoutedEvent.Name });

In the background, the compiler creates an Anonymous class, that contains two properties Sender and RoutedEventName. So in IL you’ll find a class created from the anonymous type used above. But that’s not your problem, your C# Code itself gets much shorter.

The cool thing about C# 3.0 and Visual Studio 2008 is that you can use these C# 3.0 features to build .NET 2.0 applications (Of course for WPF applications you need at least version 3.0 of .NET). But be careful when using C# 3.0 features for .NET 2.0 apps. E.g. language enhancement features like LINQ are based on Extension Methods located in assemblies introduced with .NET 3.5. :-)

Share this post

Comments (4)

  • Wasabi Reply


    Ich hab gerade versucht, dein Beispiel nachzuvollziehen, aber aus irgendeinem Grund zeigt mir das ListView keine Daten an (im Grunde habe ich exakt dasselbe gemacht wie du weiter oben, dh. die Klasse angelegt, die DisplayMemberBindings im XAML festgelegt). Dann adde ich eine Instanz dieser Klasse zur Items collection, aber die Texte sind …unsichtbar. Das Item ist selektierbar, aber komplett unbeschriftet.

    Irgendeine Idee, woran das haengt?

    February 11, 2008 at 9:25 am
  • Wasabi Reply

    Ok, Edit: Ich hatte die (quasi) Dummy-Klasse von ListViewItem vererbt, damit ich die visuellen Eigenschaften im Nachhinein manipulieren kann, aber das mag ListView dann wohl nicht, es sei denn, Du hast eine Idee, wie man zb. die Schrift- und andere Farben aendern kann, wenn man Deinen Ansatz nimmt.

    February 11, 2008 at 9:31 am
  • DaEmperor Reply


    Ich bin relativ neu in dem Gebiet und habe nach 2 Stunden des Googlens aufgegeben. Wenn ich nun ein solches ListView mit Daten gefüttert habe, wie kann ich die denn dann wieder auslesen?

    Danke schonmal für die (hoffentlich) kommende Antwort.

    February 28, 2008 at 3:39 am
  • hubethom Reply

    @Wasabi: Schrift und Farben kannst Du ändern, indem Du beispielsweise einen Style für ListViewItems erstellst. In meinem kommenden WPF-Buch findest Du dafür einige Beispiele.

    @DaEmperor: Du kannst einfach mit foreach durch die in der Items-Property gespeicherten Objekte gehen. Wenn Du zur Items-Property allerdings wie in diesem Beispiel anonyme Typen hinzugefügt hast, dann besitzt Du keine Klasse, in die Du die Objekte casten kannst, um auf die Properties zuzugreifen. Eine Möglichkeit, um dennoch an die Property-Werte der anonymen Typen ranzukommen, ist Reflection:

    foreach (object item in listView.Items)
      string senderName = "";
      string routedEventName = "";
      foreach (PropertyInfo info in 
        if (info.Name == "RoutedEventName")
          senderName = (string)info.GetValue(item, null);
        else if (info.Name == "Sender")
          routedEventName = (string)info.GetValue(item, null);
      // Hier etwas mit den beiden Variablen
      // senderName und routedEventName tun
    February 28, 2008 at 8:48 am

Leave a Reply

Your email address will not be published. Required fields are marked *