views:

212

answers:

1

I want to create a usercontrol that takes lists of different objects. These objects would be assigned to the control at design time. Now I want to be able to use linq to object to sort this list inside the usercontrol. Can anyone give me any ideas as how to go about it?

+2  A: 

Add a DependencyProperty of type ObservableCollection<T> to your user control class (call it MyItemsSource for example). In your containing XAML, bind that property to your Linq collection, and inside your user control, bind your ListBox (or other ItemsControl) to the property as follows:

{Binding 
    RelativeSource={RelativeSource 
                        Mode=FindAncester, 
                        AncestorType=UserControl}, 
    Path=MyItemsSource}

Alternatively, you can set the Name property inside the user control on the top level element (the UserControl element) to for example MyUserControl, and bind against an ElementName instead of a RelativeSource as such:

{Binding ElementName=MyUserControl, Path=MyItemsSource}
Aviad P.
but wouldnt I have to declare it like Public Shared NickNamesProperty As DependencyProperty = _DependencyProperty.Register("NickNames", GetType(ObservableCollection(Of MyObjectOne)), GetType(MyType))but at the time of creating the control,I have no idea about the type of myobject. I want to make it flexible and somehow bind lists of different type of objects.
Farax
Oh yes, this is of course for a generic type with a specified argument. If you want to be able to pass in a collection of any object, you will have to set it as `ObservableCollection(Of Object)`
Aviad P.