How To Add A Calculated Column to A Data-Bound Listview?
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.开发者_如何学JAVA Any ideas?
Thx in advance!
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();
}
精彩评论