WPF Custom Control: DependencyProperty of Collection type?

Change type of ListSource to IEnumerable, then you can bind to any collection.

Up vote 2 down vote favorite share g+ share fb share tw.

I have a Custom Control which has a ListBox: I Bind the ItemsSource with a property in the Code Behind: public partial class CustomList : UserControl, INotifyPropertyChanged { public CustomList( ) { InitializeComponent( ); } public ObservableCollection ListSource { get { return (ObservableCollection)GetValue( ListSourceProperty ); } set { base. SetValue(CustomList. ListSourceProperty, value); NotifyPropertyChanged( "ListSource" ); } } public static DependencyProperty ListSourceProperty = DependencyProperty.

Register( "ListSource", typeof( ObservableCollection ), typeof( CustomList ), new PropertyMetadata( OnValueChanged ) ); private static void OnValueChanged( DependencyObject d, DependencyPropertyChangedEventArgs e ) { ( (CustomList)d ). ListSource = (ObservableCollection)e. NewValue; } public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged( string propertyName ) { if(PropertyChanged!

= null) { PropertyChanged( this, new PropertyChangedEventArgs( propertyName ) ); } } } Now in my main Window I try to bind an ObservableCollection of "Articles" with my Custom Control and its ListSource DependencyProperty: And the error I get: Error: 1 : Cannot create default converter to perform 'one-way' conversions between types 'System.Collections.ObjectModel. ObservableCollection`1WpfApplication1. Article' and 'System.Collections.ObjectModel.

ObservableCollection`1System. Object' If in the Custom Control I have ObservableCollection instead of ObservableCollection it works. So is there a way I can bind my Custom Control's DependencyProperty with an ObservableCollection of objects without having to specify the object's type?

C# wpf custom-controls dependency-properties link|improve this question edited Apr 14 '11 at 16:37David Yaw4,977622 asked Apr 14 '11 at 13:35Aris253.

You do not need to (should not in fact) implement INotifyPropertyChanged on DependencyProperties, also do not place any code in the setter of a dependency proeprty. – H.B. Apr 14 '11 at 13:52.

Thanks for the quick response, you totally saved the day. – Aris Apr 14 '11 at 14:04.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions