views:

365

answers:

1

In Xaml, suppose I have a CollectionViewSource who's source is set to observable collection of objects of type Order:

public class Order
{
    public int ContractID
    {
        get;
        set;
    }
    public double? Price
    {
        get;
        set;
    }
    public OrderSide Side
    {
        get;
        set;
    }
}
public enum OrderSide
{
    BID,
    ASK
}

A Bid order is an order who's Side property is BID, ask is ASK. The problem is that I need the orders to be displayed in the current format

Grouped by ContractID
    Bids (Descending)      Asks (Ascending)

More specifically, the problem is that I need the Bids and Asks to be displayed side by side.

So, for example with the following collection,

var orders = new List<Order>()
        {
            new Order(){ContractID=1,Price=10,Side=OrderSide.BID},
            new Order(){ContractID=1,Price=11,Side=OrderSide.ASK},
            new Order(){ContractID=1,Price=9,Side=OrderSide.BID},
            new Order(){ContractID=1,Price=11.5,Side=OrderSide.ASK},
            new Order(){ContractID=2,Price=20,Side=OrderSide.BID},
            new Order(){ContractID=2,Price=24,Side=OrderSide.ASK},
            new Order(){ContractID=2,Price=21,Side=OrderSide.BID}
        };

It would be rendered as

1
 10    11
 9     11.5

2
 21    24
 20

I'm guessing i need to use a combination of a filtered CollectionViewSource and datatemplateselectors..?

As an added bonus, i'd like to be able to display the best bid and best ask on the grouping row. So, using the same data, it'd be rendered as

1 10    11
  10    11
  9     11.5

2 21    24
  21    24
  20

Any help much appreciated -

A: 

Quick and dirty--wrap your Orders in a class that expresses your grouping logic

public class OrdersByContractId
{
  public int ContractId {get;set;}
  // set best bid and ask when this is updated
  public ObservableCollection<Order> Orders {get;set;}
  public Order BestBid {get;set;}
  public Order BestAsk {get;set;}
}

Then you can query your repository, generate and group your orders, determine the best bids and asks and fill out your OrdersByContractIds (this could be done using Linq to Sql, but its a tad bit complicated for this answer). Then all you have to do is bind to the collection of these objects and you're done.

Will
Thanks for that- would do the trick. Guess i'm hoping to keep the underlying datasource as simple as possible ( ObservableCollection<Order> ) and let some databinding wizardry take the place of procedural code.