This should not be too difficult to set up.
public class Product
{
public string Name { get; set; }
public double Price { get; set; }
}
namespace ListViewSelect
{
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
public class ProductsVM : INotifyPropertyChanged
{
private ObservableCollection<product> itemsSource;
public ObservableCollection<product> ItemsSource
{
get
{
return this.itemsSource;
}
set
{
this.itemsSource = value;
this.NotifyPropertyChanged("ItemsSource");
}
}
private Product selectedProduct;
public Product SelectedProduct
{
get
{
return this.selectedProduct;
}
set
{
this.selectedProduct = value;
this.NotifyPropertyChanged("SelectedProduct");
}
}
private double totalPrice;
public double TotalPrice
{
get
{
return this.totalPrice;
}
set
{
this.totalPrice = value;
this.NotifyPropertyChanged("TotalPrice");
}
}
public ICommand AddProductCommand { get; private set; }
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public ProductsVM()
{
this.AddProductCommand = new DelegateCommand<object>(this.AddProduct);
this.itemsSource = new ObservableCollection<product>
{
new Product { Name = "A", Price = 1 },
new Product { Name = "B", Price = 2 },
new Product { Name = "C", Price = 3 }
};
this.itemsSource.CollectionChanged += this.itemsSource_CollectionChanged;
this.updateTotalPrice();
}
private void AddProduct(object sender)
{
this.ItemsSource.Add(new Product { Name = "Z", Price = 20 });
}
private void itemsSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
this.updateTotalPrice();
}
private void updateTotalPrice()
{
double total = this.itemsSource.Sum(p => p.Price);
this.TotalPrice = total;
}
}
}
<window x:class="ListViewSelect.MainWindow" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
xmlns:local="clr-namespace:ListViewSelect" >
<!--xmlns:local is a reference to the namespace in which the view model is defined-->
<window.datacontext>
<local:productsvm xmlns:local="#unknown" />
</window.datacontext>
<grid>
<stackpanel>
<textblock text="{Binding TotalPrice}" margin="5,0,0,0" fontsize="21.333" fontfamily="Tahoma" fontweight="Bold" />
<listview itemssource="{Binding ItemsSource}" selecteditem="{Binding SelectedProduct, Mode=TwoWay}" issynchronizedwithcurrentitem="True">
HorizontalAlignment="Center" Height="168" VerticalAlignment="Top" Width="400" Background="#FFBFBFBF">
<listview.view>
<gridview x:name="orderDetailsGridView">
<gridviewcolumn width="280" header="Product Name" displaymemberbinding="{Binding Path=Name}" />
<gridviewcolumn width="60" header="Price" displaymemberbinding="{Binding Path=Price}" />
</gridview>
</listview.view>
</listview>
<textblock text="{Binding SelectedProduct.Name}" margin="5,0,0,0" fontsize="21.333" fontfamily="Tahoma" fontweight="Bold" />
<button command="{Binding AddProductCommand}">Add</button>
</stackpanel>
</grid>
</window>
</product></object></product></product>