Rene Sá
Rene Sá

Reputation: 4

How sum the listbox items

I have a listbox with three values. I need get the FEstado value for all items of ListBox and sum them. However, this ListBox not have defined items, the user select items from another ListBox.

My Listbox:

<ListBox Name="List2" HorizontalContentAlignment="Stretch" Grid.ColumnSpan="3" Margin="0,43,-66,0">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Margin="0,0,0,17" Width="432" Height="95" Hold="holdListRmv">
                <TextBlock Grid.Column="0" Text="{Binding FNome}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                <TextBlock Grid.Column="1" Text="{Binding FEstado}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                <TextBlock Grid.Column="2" Text="{Binding Quantity}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Upvotes: 0

Views: 453

Answers (2)

Amit Bhatiya
Amit Bhatiya

Reputation: 2621

You can do something like this, try this:

XAML:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="0">
    <StackPanel>
        <ListBox Name="List2" HorizontalContentAlignment="Stretch" Grid.ColumnSpan="3" Margin="0,43,-66,0">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="0,0,0,17" Width="432" Height="95" >
                        <TextBlock Grid.Column="0" Text="{Binding FNome}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                        <TextBlock Grid.Column="1" Text="{Binding FEstado}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                        <TextBlock Grid.Column="2" Text="{Binding Quantity}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button Content="sum" Click="Button_Click_1"></Button>
    </StackPanel>
</Grid>

cs:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    ObservableCollection<DataClass> obj = new ObservableCollection<DataClass>();
    obj.Add(new DataClass("AA", "10", "10"));
    obj.Add(new DataClass("BB", "10", "10"));
    List2.ItemsSource = obj;
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    int sum = 0;
    for (int i = 0; i < List2.Items.Count; i++)
    {
        DataClass obj = (DataClass)List2.Items[i];
        sum += int.Parse(obj.FEstado.ToString());
    }
    MessageBox.Show(sum.ToString());
}

public class DataClass
{
    public string FNome { get; set; }
    public string FEstado { get; set; }
    public string Quantity { get; set; }
    public DataClass() { }

    public DataClass(string FNome, string FEstado, string Quantity)
    {
        this.FNome = FNome;
        this.FEstado = FEstado;
        this.Quantity = Quantity;
    }
}

Upvotes: 1

har07
har07

Reputation: 89325

Assuming that you populate the ListBox by adding model item to ListBox's Items property (as shown in your previous question), then you can get all items from the same ListBox.Items property. And assuming that FEstado is a number, you can do something like this :

var items = List2.Items.Cast<Fields>();
var total = items.Sum(o => o.FEstado);

Upvotes: 2

Related Questions