Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have a class that looks like this:
public class ListProduct{
 public string productName { get; set;}
 public double pricePerUnit { get; set;}
 public int number {get; set;}
 public double totalPrice {get; set;}
}
and I have ListProducts that is a collection of ListProduct.

When I click on a button I add a ListProduct to my ListProducts and I update my listview this way:
myListView.datasource= null;
myListView.datasource= ListProducts ;
It's too stupid I know so I'm asking if there is another why to do that? I mean
in my list I have 5 product (it mean that in my ListProducts I have 5 product) when I add a new product into my ListProducts I'd like myListView to update dynamically.
Posted
Updated 6-Mar-10 10:42am
v2

Thank's great work :)
sorry for taking to much from your time
but you know I'm beginner in WPF I'm trying to survive here :)
 
Share this answer
 
The way to do this is to use something that automatically notifies attached ItemControls that it has updated when an item is added or removed. If only .NET provided a collection that was observed by something; gosh do you think that .NET could actually provided something like this; something like an ObservableCollection<ListProduct>. Google for ObservableCollection and WPF and you'll find shed loads of examples on how to do this.
 
Share this answer
 
Thanks for you help
I have this code
when I click on the button if the element exist on my list so I just modify the number if not I add it

when I add the elemnt myList is updated but when I click second time to modfy number it does not changes any explination please?
<pre>ObservableCollection<ListProduct> myList = new ObservableCollection<ListProduct>();
private void Button_Click(object sender, RoutedEventArgs e)
{
int index = myList.IndexOf((from par in myList
where par.name ="..."
select par).FirstOrDefault());
if(index != -1)
{
myList[index].number++;

}
else
{
myList.Add(new ListProduct { name = "...", number = 1 });
}
}

public class ListProduct{
public string productName { get; set;}
public double pricePerUnit { get; set;}
public int number {get; set;}
public double totalPrice {get; set;}
}</pre>
code Xaml
<ListView Name="ListView12" ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridViewColumn Header="name"
DisplayMemberBinding="{Binding name}">
<GridViewColumn Header="name"
DisplayMemberBinding="{Binding number}">
</GridView>
<ListView.View>
</ListView>
 
Share this answer
 
v7
You shouldn't really add a question to your question. You could edit your question to add the extra info.

Anyway, the answer to your problem lies in the fact that you've confused a notification of a change to the collection with a notification of a change to a property. The problem that you are facing is that you need to change ListProduct so that it implements INotifyPropertyChanged like this:
public class ListProduct : INotifyPropertyChanged
{
  private string _productName;
  private double _pricePerUnit;
  private int _number;
  private double _totalPrice;

  public string ProductName
  {
    get { return _productName; }
    set {
      if (_productName != value)
      {
        _productName = value;
        OnChanged("ProductName");
      }
    }
  }

  // You get the idea about how to handle the other properties in here.
  public event PropertyChangedEventHandler PropertyChanged;
  private void OnChanged(string property)
  {
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
      handler(this, new PropertyChangedEventArgs(property));
  }
}
 
Share this answer
 
You didn't take too much time. Don't worry about it; we are here to help after all. :-D
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900