views:

159

answers:

1

I want include the Type name of each object in my collection from my GridView. I have a collection which has four different types in it that all derive from a base class. Call them, Foo, Bar, Fizz, and Buzz.

I want to have that column read, Foo, Bar, Fizz or Buzz, respectively. Below is the Binding I'm using, however, it doesn't work.

So far I have this.

GridViewColumn colC = new GridViewColumn()
{
Header = "Type",
Width = 100,
DisplayMemberBinding = New Binding("listName.GetType().Name")
}

It does work if I use a String property called TypeName which called GetType().Name for me.

Any ideas? Am I explaining it correctly?

+2  A: 

You can't invoke a method using a binding. You must either use a wrapper property (as per your TypeName solution) or a converter e.g.

public class TypeNameConverter : IValueConverter
{
  public object Convert(object value, ...)
  {
    return value.GetType().Name;  // omitting error handling
  }
}

DisplayMemberBinding = new Binding("listName") { Converter = new TypeNameConverter() };

The converter approach obviously has the benefit that it doesn't require you to modify your model.

itowlson
@itowlson, Yes, you are correct, I've in the process of implementing an MVVM.
Chris