views:

26

answers:

2

I'm binding a GridView to a collection of objects that look like this:

public class Transaction
{
   public string PersonName { get; set; }
   public DateTime TransactionDate { get; set; }
   public MoneyCollection TransactedMoney { get; set;}
}

MoneyCollection simply inherits from ObservableCollection<T>, and is a collection of MyMoney type object.

In my GridView, I just want to bind a column to the MoneyCollection's ToString() method. However, binding it directly to the TransactedMoney property makes every entry display the text "(Collection)", and the ToString() method is never called.

Note that I do not want to bind to the items in MoneyCollection, I want to bind directly to the property itself and just call ToString() on it.

I understand that it is binding to the collection's default view. So my question is - how can I make it bind to the collection in such a way that it calls the ToString() method on it?

This is my first WPF project, so I know this might be a bit noobish, but pointers would be very welcome.

+1  A: 

Write a IValueConverter implementation that calls ToString() on the bound collection and returns it and use this converter in the XAML binding expression.

bitbonk
I'll do this if I have to. It seems like there should be a simpler way?
womp
I ended up going this route.
womp
+1  A: 

You can add property StringRepresentation or something like this in MyMoney class. If you do not want to affect this class, you should write a wrapper - MyMoneyViewModel which will have all needed properties. This is a common way. HIH!

levanovd
public string AsString { get { return this.ToString(); } } or something. Then bind to AsString.
Joel