Implement Binding List In a Control
I have a (Image) User Control on a Winform ..
I want to bind this Control to a binding list such that whenever the (images) collection changes th开发者_如何学Goe (images showing on the ) control also change. (should reflect the images within the collection). [something similar to an AsyncBinding List.] .Similar to how .net controls use DataSource Property.
//[EDITED] need Binding list.
BindingList<Image> _images = GetImages("folder_path");
ImageControl ctrl = new ImageControl();
ctrl.DataSource = _images; //something similar
How can i achieve the same?
EDIT2:
Basically I want to display Images in a List..Something similar to how picasa displays it. (source: aiostudents.com)Thanks All
You'll want to use a BindingList, rather than a List or Collection. The key difference is that the BindingList supports INotifyPropertyChanged, which is required to perform two-way databinding. Without that interface you can initially bind to the list, but changes to the list won't reflect in the UI.
As STW already mentioned you need a BindingList<T>
so that a change within the list will be propagiated into the BindingSource.
The next step is how will your UserControl being notified about changes in the BindingSource. To solve this problem add this code as starting point to your UserControl.cs:
private BindingSource _DataSource;
[TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design")]
[Editor("System.Windows.Forms.Design.DataSourceListEditor, System.Design", typeof(UITypeEditor))]
[AttributeProvider(typeof(IListSource))]
public object DataSource
{
get
{
return _DataSource;
}
set
{
//Detach from old DataSource
if (_DataSource != null)
{
_DataSource.ListChanged -= _DataSource_ListChanged;
}
_DataSource = value as BindingSource;
//Attach to new one
if (_DataSource != null)
{
_DataSource.ListChanged += _DataSource_ListChanged;
//ToDo: look for other (maybe usable) BindingSource events
//http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource_events.aspx
}
}
}
void _DataSource_ListChanged(object sender, ListChangedEventArgs e)
{
//ToDo: Reacht on specific list change
switch (e.ListChangedType)
{
case ListChangedType.ItemAdded:
break;
case ListChangedType.ItemChanged:
break;
case ListChangedType.ItemDeleted:
break;
case ListChangedType.ItemMoved:
break;
case ListChangedType.PropertyDescriptorAdded:
break;
case ListChangedType.PropertyDescriptorChanged:
break;
case ListChangedType.PropertyDescriptorDeleted:
break;
case ListChangedType.Reset:
break;
default:
break;
}
}
With this you will be informed if anyone makes changes to the BindingSource itself (exchange the whole list against a new one) or on the list of elements attached to the BindingSource.
Also you should really good test it, cause some controls have a little weird usage of lists (e.g. instead of changing some elements on a list, clear it and fill it up from scratch).
Maybe this Walkthrough from Microsoft can help, too. I didn't read it, but it looks promising.
精彩评论