Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. How to bind a List to a ListBox in a UserControl

How to bind a List to a ListBox in a UserControl

Scheduled Pinned Locked Moved WPF
csharpwpfquestiondatabasevisual-studio
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    davidnhilda
    wrote on last edited by
    #1

    Hi I have a very basic WPF question. Please forgive my ignorance. I have Visual Studio Solution with two VB Projects: Project A is a WPF program that contains x, x being an ObservableCollection(of Rec) where rec is a Class of some database record. Project B is a UserControl uc, containing, amongst other things a ListBox lb. The problem is how to display the x items in lb. Project A contains several instances of uc. Possible solution 1: Is there a way that I can bind lb directly to x? I seem to be unable to do so. Possible solution 2: Should I define a dependency property of ObservableCollection(of Rec) in B and give that a value in the Xaml of the uc instances. This also fails! Conceptually, this method has me worried, because I cannot see how uc obtains the rec info for formatting the records in its DataTemplate. I have to define the Property as a ObservableCollection(of Object) to get past the compiler! Possible solution 3. It cannot be done! BTW, I know enough to display x in a listbox defined in A quite nicely with a DataTemplate. I have searched Google long and hard without finding anything that throws light on my dilemma! Any insight will be much appreciated. David

    A 1 Reply Last reply
    0
    • D davidnhilda

      Hi I have a very basic WPF question. Please forgive my ignorance. I have Visual Studio Solution with two VB Projects: Project A is a WPF program that contains x, x being an ObservableCollection(of Rec) where rec is a Class of some database record. Project B is a UserControl uc, containing, amongst other things a ListBox lb. The problem is how to display the x items in lb. Project A contains several instances of uc. Possible solution 1: Is there a way that I can bind lb directly to x? I seem to be unable to do so. Possible solution 2: Should I define a dependency property of ObservableCollection(of Rec) in B and give that a value in the Xaml of the uc instances. This also fails! Conceptually, this method has me worried, because I cannot see how uc obtains the rec info for formatting the records in its DataTemplate. I have to define the Property as a ObservableCollection(of Object) to get past the compiler! Possible solution 3. It cannot be done! BTW, I know enough to display x in a listbox defined in A quite nicely with a DataTemplate. I have searched Google long and hard without finding anything that throws light on my dilemma! Any insight will be much appreciated. David

      A Offline
      A Offline
      Abhishek Sur
      wrote on last edited by
      #2

      Hey David, Working with listbox is very easy. You put the object in Resource, say your class which has the ObservableCollection is in X.

      class X
      {
      public ObservableCollection { get;set;} ....
      }

      in XAML

      So here the property MyText will be displayed in the list of MyObject.

      You can also use ICollectionView to do this if you wish. Check my article on it :
      http://www.abhisheksur.com/2010/08/woring-with-icollectionviewsource-in.html[^]

      :rose:

      Abhishek Sur
      **Don't forget to click "Good Answer" if you like this Solution.


      Visit My Website-->**

      www.abhisheksur.com

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups