locked
Listbox problem when binding to a observableCollection RRS feed

  • Question

  • Hello everyone. I was binding an observable collection to a listbox with no problem, everything was displaying fine.But then i realized that i had forgotten to implement the iNotifyPropertyChanged for the class that my observablecollection holds. i did this because i wanted the changes made to the collection to reflect back on the listbox. So i implemented this and behold the listbox stopped displaying data. Can anyone point out whats wrong i dont understand what im doing incorrectly. Thanks in advance to everyone.

     

    The XAML

    ---------------------------------------------------------------------------------------------

    <ListBox  Height="200" x:Name="testList" ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel  />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Border Width="75" BorderBrush="Black" Margin="5" BorderThickness="1">
                           <StackPanel>
                                <TextBox Margin="1" BorderThickness="0.5" BorderBrush="Red" Height="22"  Text="{Binding Path= FirstName}" />
                                <TextBox Margin="1" BorderThickness="0.5" BorderBrush="Red" Height="22"  Text="{Binding Path= LastName}" />
                                <TextBox Margin="1" BorderThickness="0.5" BorderBrush="Red" Height="22"  Text="{Binding Path= City}" />
                            </StackPanel>
                        </Border>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

     

    THE CLASS that implements the iNotifyPropertyChanged interface

    ---------------------------------------------------------------------------------------------

    public class Class1 :INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;

            public Class1()
            {
            }

            string fname;
            public string FirstName
            {
                get
                {
                    return fname;
                }
                set
                {
                    fname = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
                }
            }

            string lname;
            public string LastName
            {
                get
                {
                    return lname;
                }
                set
                {
                    lname = value;
                   PropertyChanged(this, new PropertyChangedEventArgs("LastName"));
                }
            }

            string c;
            public string City
            {
                get
                {
                    return c;
                }
                set
                {
                    c = value;
                   PropertyChanged(this, new PropertyChangedEventArgs("City"));
                }
            }     
        }

     

    THis is how i give the itemsSource to the list

    ---------------------------------------------------------------------------------------------

     

    ObservableCollection<Class1> lst = new ObservableCollection<Class1>();

    //Some dummy data

     for (int i = 0; i <= 100; i++)
                {
                    Class1 c = new Class1() { FirstName = "blah blah blah", LastName = "who knows", City = "Miami" };
                    lst.Add(c);
                }

     

    //bind

    this.testList.ItemsSource = lst;

     

     

    If i dont implement the InotifyPropertYChanged interface in Class1 everything works fine so im pretty sure the error is in class 1 but i dont know what i did wrong implementing the inotifypropertychanged interface.

     

    Again thanks in advance

    Friday, July 15, 2011 5:07 PM

Answers

  • Hello

    I think your implementation of InotifyPropertyChanged is wrong. Here i support u one very small class implemeting the interface.

     class DeliveryContent : INotifyPropertyChanged
      {
        public event PropertyChangedEventHandler PropertyChanged;
    
        private void OnPropertyChanged(String info)
        {
          if (PropertyChanged != null)
          {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
          }
        }
    
      
    
        private string barcode = "";
        public string Barcode
        {
          get
          {
            return barcode;
          }
          set
          {
            barcode = value;
            this.OnPropertyChanged("Barcode");
          }
        }
    }
    

    Hope that helps good luck with coding!

    Dimitar.

    • Proposed as answer by _sanguine_ Saturday, July 16, 2011 4:25 AM
    • Marked as answer by salserito6780 Saturday, July 16, 2011 6:13 PM
    Friday, July 15, 2011 5:35 PM
  • Hello,

    I think you should check for not null before call PropertyChanged:

     

     

       string c;
      public string City
      {
       get
       {
        return c;
       }
       set
       {
        c = value;
         if (PropertyChanged != null)
         PropertyChanged(this, new PropertyChangedEventArgs("City"));
       }
      } 
    

    Good Luck

     

     


    • Proposed as answer by _sanguine_ Saturday, July 16, 2011 4:25 AM
    • Marked as answer by salserito6780 Saturday, July 16, 2011 6:13 PM
    Friday, July 15, 2011 5:47 PM

All replies

  • Hello

    I think your implementation of InotifyPropertyChanged is wrong. Here i support u one very small class implemeting the interface.

     class DeliveryContent : INotifyPropertyChanged
      {
        public event PropertyChangedEventHandler PropertyChanged;
    
        private void OnPropertyChanged(String info)
        {
          if (PropertyChanged != null)
          {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
          }
        }
    
      
    
        private string barcode = "";
        public string Barcode
        {
          get
          {
            return barcode;
          }
          set
          {
            barcode = value;
            this.OnPropertyChanged("Barcode");
          }
        }
    }
    

    Hope that helps good luck with coding!

    Dimitar.

    • Proposed as answer by _sanguine_ Saturday, July 16, 2011 4:25 AM
    • Marked as answer by salserito6780 Saturday, July 16, 2011 6:13 PM
    Friday, July 15, 2011 5:35 PM
  • Hello,

    I think you should check for not null before call PropertyChanged:

     

     

       string c;
      public string City
      {
       get
       {
        return c;
       }
       set
       {
        c = value;
         if (PropertyChanged != null)
         PropertyChanged(this, new PropertyChangedEventArgs("City"));
       }
      } 
    

    Good Luck

     

     


    • Proposed as answer by _sanguine_ Saturday, July 16, 2011 4:25 AM
    • Marked as answer by salserito6780 Saturday, July 16, 2011 6:13 PM
    Friday, July 15, 2011 5:47 PM
  • Hi guys thanks for the feedback, i tried what you guys suggested but still no luck.
    Saturday, July 16, 2011 5:56 PM
  • Hey guys you where right my implementation was wrong. this fixed the problem...

     

    public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }

     

    then to notify the update do something like this

     

     NotifyPropertyChanged("FirstName");

     

    basically what the first guy suggested.. thanks guys

    Saturday, July 16, 2011 6:13 PM