views:

162

answers:

1

Hi!

I have a WPF ListView with several columns bound to a custom collection. The question is, how can I add another column which is calculated, not bound. Specifically, this is what I have, which displays fine:

            <ListView Name="ui_rptTransactions">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Date" 
                                        DisplayMemberBinding="{Binding Path=Date}" />
                        <GridViewColumn Header="Category" 
                                        DisplayMemberBinding="{Binding Path=Category.Name}" />
                        <GridViewColumn Header="Amount" 
                                        DisplayMemberBinding="{Binding Path=Amount}" />
                    </GridView>
                </ListView.View>
            </ListView>

In the next column, I'd like to have a "Balance", which is calculated as "The last manually confirmed balance" minus "the sum of all preceeding transaction amounts". The first part of that is available via: theBank.LastActualBalance.Amount public property. (The above ListView is bound to theBank.Transactions, an ObservableCollection.

...but I'm a bit stuck as to how to do that. Any ideas?

Thx in advance!

+1  A: 

Your last column can be a bind just like the rest of the columns. You just have to do the calculation in the code behind.

I would Bind the last column "Balance" to a dependency which is calculated off of the CollectionChanged event of Transactions. That way whenever a Transaction is made an event is fired that recalculates your Dependency Balance.

oCollection.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(oCollection_CollectionChanged);

private void oCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
    CalculatedBalance();
}
jsmith
Thx. I'm just a bit disappointed that there's not an easier way, grrr.
Gregg Cleland