none
adding listbox item to listbox using c# in wpf

    Question

  • i'm making an application in which i have to fill my listbox on form load with listbox item.

    How to do this in wpf. i have made a content template for that listboxitem. how to add that template to new listbox item?


    Regards Kumar Gaurav.
    Wednesday, December 29, 2010 4:31 PM

Answers

  • A Listbox control is know as an Items control.  This means that you can fill it with a collection of ITEMS of any type.  I almost always use Binding in Code-Behind to do this.  In Brian's example above you see a Binding statement which loads the text portion of each listbox item.  That Binding is the equivalent of

    "Foreach (thing in my collection) fill the listbox item's textbox with the value of firstname from current thing"

    Binding has two parts CONTEXT and PATH, If you look at view models in WPF you see the context will be the class to bind to, the path then is the property within the class.

    I highly recommend you look into the MVVM pattern.  If you are going to be using WPF this is the best way to do it.

     

     

     


    Javaman
    • Marked as answer by urprob Thursday, December 30, 2010 2:37 PM
    Thursday, December 30, 2010 11:54 AM
  • it is an ObersvableCollection of objects, in my example I am using a Person object:

     public class Person : INotifyPropertyChanged
     {
      private string _firstName;
      public string FirstName
      {
       get { return _firstName; }
       set
       {
        _firstName = value;
        OnPropertyChanged("FirstName");
       }
      }
    
      private string _lastName;
      public string LastName
      {
       get { return _lastName; }
       set
       {
        _lastName = value;
        OnPropertyChanged("LastName");
       }
      }
    
      public Person() { }
    
      #region INotifyPropertyChanged Members
    
      public event PropertyChangedEventHandler PropertyChanged;
      public void OnPropertyChanged(string propertyName)
      {
       if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
    
      #endregion
     }
    

    Then in the Window load event I populate the collection.

    void Window1_Loaded(object sender, RoutedEventArgs e)
    {
     ObservableCollection<Person> collection = new ObservableCollection<Person>();
    
     for (int i = 0; i < 10; i++)
     {
      collection.Add(new Person() { FirstName = String.Format("First {0}", i), LastName = String.Format("Last{0}", i) });
     }
    
     _listBox.ItemsSource = collection;
    }
    

    hope this helps.

    • Marked as answer by urprob Thursday, December 30, 2010 2:37 PM
    Thursday, December 30, 2010 2:12 PM

All replies

  • First create a DataTemplate as a resource (this defines what you want your items to look like when displayed):

     <Window.Resources>
      <DataTemplate x:Key="ListBoxTemplate">
       <TextBlock Text="{Binding FirstName}" Foreground="Red" />
      </DataTemplate>
     </Window.Resources>
    

    Then use it on the ListBox by setting the ItemTemplate property to your resource:

    <ListBox x:Name="_listBox" ItemTemplate="{StaticResource ListBoxTemplate}" />
    

    Then if using code behine set the ItemsSource to your data:

    _listBox.ItemsSource = collection;
    
    • Proposed as answer by Mr. Javaman Wednesday, December 29, 2010 4:57 PM
    Wednesday, December 29, 2010 4:43 PM
  • Mr. Javaman,

    I think you read too quickly.  There is no mistake.  The DataTemplate is used as a ItemTemplate for the ListBox.

    He also mentioned he wanted to set the ItemsSource on form load which means he will not be binding to the ItemsSource property in XAML, but rather setting it in code behind.
    Wednesday, December 29, 2010 5:00 PM
  • Sorry Brian you are correct, your example is perfect.  I had indeed misread the first post, thinking he said

     "fill my listbox on form load with listbox"... Besides it doesn't make sense.  I must have been in la-la land.....

    I removed post


    Javaman
    Wednesday, December 29, 2010 11:02 PM
  • Sorry Brian you are correct, your example is perfect.  I had indeed misread the first post, thinking he said

     "fill my listbox on form load with listbox"... Besides it doesn't make sense.  I must have been in la-la land.....

    I removed post


    Javaman

    I want to fill listbox with listboxitems as i have written in my first post not with listbox itself.

    again what is collection there in code behind.

    i'm new to wpf. please explain


    Regards Kumar Gaurav.
    Thursday, December 30, 2010 11:02 AM
  • A Listbox control is know as an Items control.  This means that you can fill it with a collection of ITEMS of any type.  I almost always use Binding in Code-Behind to do this.  In Brian's example above you see a Binding statement which loads the text portion of each listbox item.  That Binding is the equivalent of

    "Foreach (thing in my collection) fill the listbox item's textbox with the value of firstname from current thing"

    Binding has two parts CONTEXT and PATH, If you look at view models in WPF you see the context will be the class to bind to, the path then is the property within the class.

    I highly recommend you look into the MVVM pattern.  If you are going to be using WPF this is the best way to do it.

     

     

     


    Javaman
    • Marked as answer by urprob Thursday, December 30, 2010 2:37 PM
    Thursday, December 30, 2010 11:54 AM
  • it is an ObersvableCollection of objects, in my example I am using a Person object:

     public class Person : INotifyPropertyChanged
     {
      private string _firstName;
      public string FirstName
      {
       get { return _firstName; }
       set
       {
        _firstName = value;
        OnPropertyChanged("FirstName");
       }
      }
    
      private string _lastName;
      public string LastName
      {
       get { return _lastName; }
       set
       {
        _lastName = value;
        OnPropertyChanged("LastName");
       }
      }
    
      public Person() { }
    
      #region INotifyPropertyChanged Members
    
      public event PropertyChangedEventHandler PropertyChanged;
      public void OnPropertyChanged(string propertyName)
      {
       if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
    
      #endregion
     }
    

    Then in the Window load event I populate the collection.

    void Window1_Loaded(object sender, RoutedEventArgs e)
    {
     ObservableCollection<Person> collection = new ObservableCollection<Person>();
    
     for (int i = 0; i < 10; i++)
     {
      collection.Add(new Person() { FirstName = String.Format("First {0}", i), LastName = String.Format("Last{0}", i) });
     }
    
     _listBox.ItemsSource = collection;
    }
    

    hope this helps.

    • Marked as answer by urprob Thursday, December 30, 2010 2:37 PM
    Thursday, December 30, 2010 2:12 PM