开发者

Bind to a collection's view and just call ToString() in WPF

开发者 https://www.devze.com 2023-01-01 21:55 出处:网络
I\'m binding a GridView to a collection of objects that look like this: public class Transaction { public string PersonName { get; set; }

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() met开发者_如何学Gohod. 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.


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!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消