How to use ItemPanel Data-Binding Features – WinForms
Categories: DotNetBar for Windows Forms, How To
DotNetBar for Windows Forms ItemPanel control starting with 10.1.0.6 release includes support for data-binding. To bind ItemPanel to your data source follow these steps:
- Set DataSource property to the source of your data. This can be array of custom objects or data set.
- Set ItemTemplate property to the item which will act as template for each data row from data source. The ItemTemplate is an BaseItem inherited item that is repeated for each data row. In VS.NET designer you can invoke drop-down to create desired item to use as template.
- Set ItemTemplateBindings collection with the list of bindings that are applied on the ItemTemplate. ItemTemplateBindings collection accepts BindingDef type which describes the single binding. Two key properties on BindingDef must be set: DataMember – which is name of the field from your data-source that provides the data for PropertyName – which specifies the property on ItemTemplate which receives data.
Please note that if you are changing ItemTemplateBindings collection after the DataSource is assigned, you should call RefreshItems() to re-generate the visual data representation.
You can add following code to the form which has itemPanel1 control added to it to demo the data-binding (note that code assumes your form is named Form1 and ItemPanel is named itemPanel1):
C#:
public Form1() { InitializeComponent(); // Create custom data List<CustomClass> items = new List<CustomClass>(); for (int i = 0; i < 20; i++) { items.Add(new CustomClass("Item " + i.ToString(), "Name " + i.ToString())); } // Create template used for each data row MetroTileItem template = new MetroTileItem(); template.TileColor = eMetroTileColor.Magenta; itemPanel1.ItemTemplate = template; // Define bindings itemPanel1.ItemTemplateBindings.Clear(); // Bind MetroTileItem.Text to CustomClass.Text itemPanel1.ItemTemplateBindings.Add(new BindingDef("Text", "Text")); // Bind MetroTileItem.TitleText to CustomClass.Name itemPanel1.ItemTemplateBindings.Add(new BindingDef("TitleText", "Name")); // Assign data source itemPanel1.DataSource = items; } public class CustomClass : INotifyPropertyChanged { public CustomClass() { } /// <summary> /// Initializes a new instance of the CustomClass class. /// </summary> /// <param name="text"></param> /// <param name="name"></param> public CustomClass(string text, string name) { _Text = text; _Name = name; } private string _Text; public string Text { get { return _Text; } set { _Text = value; OnPropertyChanged(new PropertyChangedEventArgs("Text")); } } private string _Name; public string Name { get { return _Name; } set { _Name = value; OnPropertyChanged(new PropertyChangedEventArgs("Name")); } } #region INotifyPropertyChanged Members /// <summary> /// Raises the PropertyChanged event. /// </summary> /// <param name="e">Provides event arguments.</param> protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, e); } /// <summary> /// Occurs when property on BindingDef object has changed. /// </summary> [Description("Occurs when property on BindingDef object has changed.Occurs when property on BindingDef object has changed.")] public event PropertyChangedEventHandler PropertyChanged; #endregion }
VB:
Public Sub New() ' Create custom data Dim items As New List(Of CustomClass)() For i As Integer = 0 To 19 items.Add(New CustomClass("Item " & i.ToString(), "Name " & i.ToString())) Next ' Create template used for each data row Dim template As New MetroTileItem() template.TileColor = eMetroTileColor.Magenta itemPanel1.ItemTemplate = template ' Define bindings itemPanel1.ItemTemplateBindings.Clear() ' Bind MetroTileItem.Text to CustomClass.Text itemPanel1.ItemTemplateBindings.Add(New BindingDef("Text", "Text")) ' Bind MetroTileItem.TitleText to CustomClass.Name itemPanel1.ItemTemplateBindings.Add(New BindingDef("TitleText", "Name")) ' Assign data source itemPanel1.DataSource = items End Sub Public Class CustomClass Implements INotifyPropertyChanged Public Sub New() End Sub ''' <summary> ''' Initializes a new instance of the CustomClass class. ''' </summary> ''' <param name="text"></param> ''' <param name="name"></param> Public Sub New(text As String, name As String) _Text = text _Name = name End Sub Private _Text As String Public Property Text() As String Get Return _Text End Get Set _Text = value OnPropertyChanged(New PropertyChangedEventArgs("Text")) End Set End Property Private _Name As String Public Property Name() As String Get Return _Name End Get Set _Name = value OnPropertyChanged(New PropertyChangedEventArgs("Name")) End Set End Property #Region "INotifyPropertyChanged Members" ''' <summary> ''' Raises the PropertyChanged event. ''' </summary> ''' <param name="e">Provides event arguments.</param> Protected Overridable Sub OnPropertyChanged(e As PropertyChangedEventArgs) Dim handler As PropertyChangedEventHandler = PropertyChanged RaiseEvent handler(Me, e) End Sub ''' <summary> ''' Occurs when property on BindingDef object has changed. ''' </summary> <Description("Occurs when property on BindingDef object has changed.Occurs when property on BindingDef object has changed.")> _ Public Event PropertyChanged As PropertyChangedEventHandler #End Region End Class Convert again
Related posts: