How to bind a List to a ListBox in a UserControl
-
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
-
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
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-->**