0

I am trying display the total value of the items selected, the selected items are in a ListView. The values of item appear in the list but I want the total value of items to display inside a textbox. My code is below:

Basket.xaml

       private static ObservableCollection<Menu.PassedData> passedData = new ObservableCollection<Menu.PassedData>();

 private double totalValue;
        public double TotalValue
        {
            get { return totalValue; }
            set
            {
                totalValue = value;
                OnPropertyChanged("TotalValue");
            }
        }

  protected override void OnNavigatedTo(NavigationEventArgs e)
{
    Menu.PassedData data = e.Parameter as Menu.PassedData;

    if (data != null) //If data is not 0 
    {
        PassedData.Add(data); //Increment data in list view

        double tempTotalValue = 0;
        foreach (var record in PassedData)
        {
            tempTotalValue = tempTotalValue + record.Value;
        }
        TotalValue = tempTotalValue;
    }
}


    }

The list in which the values are stored(Basket.xaml)

 <ListBox x:Name="listBox1" HorizontalAlignment="Left" Height="265" Margin="278,175,0,0" VerticalAlignment="Top" Width="112">
        <ListView x:Name="PriceView" ItemsSource="{Binding PassedData}" HorizontalAlignment="Left" VerticalAlignment="Center">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="auto"/>
                            <ColumnDefinition Width="auto"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="auto"/>
                        </Grid.RowDefinitions>
                        <!--<TextBox Grid.Column="0" Text="{Binding Name}" IsReadOnly="True" /> !-->
                        <TextBox Grid.Column="1" Text="{Binding Value}" IsReadOnly="True" />
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ListBox>

So basically add up the Value column in listview and display it in a textbox.

2 Answers2

0

Something like Getting WPF ListView.SelectedItems in ViewModel could be useful. Then in your command use linq to get a sum of all the values and set some property (bound) with this value.

Community
  • 1
  • 1
grmbl
  • 2,514
  • 4
  • 29
  • 54
0

There are many ways to achieve this. Using your code you may for example: In your Basket.xaml.cs add TotalValue property:

private double totalValue;
public double TotalValue
{
    get { return totalValue; }
    set
    {
        totalValue = value;
        OnPropertyChanged("TotalValue");
    }
}

And then in your OnNavigatedTo method you can just iterate through all elements of your list to get total value:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    Menu.PassedData data = e.Parameter as Menu.PassedData;

    if (data != null) //If data is not 0 
    {
        PassedData.Add(data); //Increment data in list view

        double tempTotalValue = 0;
        foreach (var record in PassedData)
        {
            tempTotalValue = tempTotalValue + record.Value;
        }
        TotalValue = tempTotalValue;
    }
}

And then just bind this value to lets say Textblock in your view Basket.xaml:

<TextBlock Text="{Binding TotalValue}"></TextBlock>
SteppingRazor
  • 1,222
  • 1
  • 9
  • 25
  • Thanks for the answer. I have implemented your answer but when I run the app the total textblock stays static-only displays 0 no matter how many items I add an item. The question is updatated. – Smith Sunny Apr 09 '16 at 20:43
  • Does your `Basket` class implement `INotifyPropertyChanged` interface? – SteppingRazor Apr 09 '16 at 20:45
  • In order to make your `OnPropertyChanged` method work you have to implement `INotifyPropertyChanged` interface, like this: `public sealed partial class Basket : Page, INotifyPropertyChanged` – SteppingRazor Apr 09 '16 at 21:09