-2

I have two ListBoxes, Source and Destination. The destination ListBox has Items selected by the user.

How do I sum items in a destination ListBox?

The Destination ListBox has two data types String (Description) and decimal (Cost). I want to sum up only the Cost. Here is the XAML Code

<ListBox Height="237" HorizontalAlignment="Left" Margin="44,191,0,0" Name="lstDestination" VerticalAlignment="Top" Width="264" Grid.ColumnSpan="2" ItemsSource="{Binding Source={StaticResource myItemList}}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <DockPanel >
                   <TextBlock FontWeight="Bold" Text="Item:"  DockPanel.Dock="Left" Margin="5,0,10,0"/>
                   <TextBlock Text="{Binding Path=Resource}" Foreground="Green" FontWeight="Bold" />
                   <TextBlock FontWeight="Bold" Text="Cost:"  DockPanel.Dock="Left" Margin="5,0,10,0"/>
                  <TextBlock Text="{Binding Path=Cost}" FontWeight="Bold" />
                </DockPanel>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

This is the code I tried to sum up Cost:

private decimal SumDLstBox()
{
    decimal totalCost;
    foreach (var i in lstDestination.SelectedItems)
    {
        totalCost+=i.Cost; //Error is thrown here             
    }
    return totalCost;
}
//Data Source to List Box Source
 var items = from r in dc.IResources select r;
        lstSource.ItemsSource = items;

The user them selects the Items s/he needs, my challenge is to get the total of the selected items(Which have been moved to the ListBox Destination)

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
love
  • 49
  • 9

3 Answers3

1

To operate on your bound data, you should cast it to a list of the datatype you're working with. For eg.

private decimal SumDLstBox()
{
    return lstDestination.SelectedItems.Cast<Foo>().Sum(f => f.Cost);
}

Where Foo is the datatype of the List you bound to the control.

Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406
0

Your .cs-file will know the property lstDestination. You can use a foreach loop to iterate through all elements in the lstDestination. In that same loop, you can add up the costing.

Joetjah
  • 6,292
  • 8
  • 55
  • 90
  • @love Well then, try something, test it, and if you don't understand why it doesn't work, lookup the Error-message on SO to see if someone asked about it before. If those answers don't help you, post a question with your code, the error message and the things you have tried. That's the way this site works. We don't create applications for you. Where do you hit the wall? What is the thing you don't understand? You really have to be more clear because I can't guess what you don't understand... – Joetjah May 21 '13 at 14:45
  • @JoeJah I understand I have edited the question with the code that I tried. – love May 21 '13 at 15:01
  • Well the error thrown is "'object' does not contain a definition for 'Cost' and no extension method 'Cost' accepting a first argument of type 'object'" – love May 21 '13 at 15:03
  • 1
    -1. This encourages the use of really bad practices. [UI is NOT Data](http://stackoverflow.com/questions/14381402/wpf-programming-methodology/14382137#14382137) – Federico Berasategui May 21 '13 at 15:08
  • True, binding to a getter/setter of the total cost calculated somewhere else seperates the UI from the data. @love You get the error because your program doesn't know what `i` is. You say it is a `var`, your program cannot automatically find the right definition. What if there are more objects which have `.Cost`? – Joetjah May 21 '13 at 15:13
  • 1
    @HighCore: +1 - I'm sorry, but I don't agree. I love the MVVM pattern too and I'm also implementing everything by sticking to it, but you cannot tell people, that their approach is wrong, because they don't use it. It's a free world and there is not only one way of implementing WPF applications. If they don't want to separate UI, logic and data, let them. Sure you can give recommendations, but that's all. – DHN May 21 '13 at 15:19
  • @DHN Sure, there are many ways to do WPF. One is MVVM, all the rest is having crappy useless unneeded thousands of lines of code manipulating the UI. I'm sorry but my duty as a developer is to tell my fellow developers NOT to write crappy code, but to strife for the best possible code quality. After all, it's us the developers who deal with (ours and others') code. Managers don't deal with code, neither do users. – Federico Berasategui May 21 '13 at 15:25
  • @HighCore Partially agreed, as I said. You can give recommendations. The developer who wants to improve his implementation skills a.s.o. will think on it. But downvoting gives a wrong signal. The solution might be ugly but it's still a solution. So I'd like to ask you to change your style of propagating our philosophy. :o) I've read some of your comments and posts, they are IMHO a bit harsh – DHN May 21 '13 at 15:34
  • @DHN Ok.. I have to admit that I'm not the most polite person in the world. I do get mad at stuff and people easily. – Federico Berasategui May 21 '13 at 15:36
0

Seems to me that you don't to iterate by lstDestination.SelectedItems but by the lstDestination.Items

sexta13
  • 1,558
  • 1
  • 11
  • 19