« Check Boxes | Index | Columns » |
Better ListView contains several types collections of its elements (columns, items, sub-items, groups). These can be accessed via properties:
Columns
Items
Groups
BetterListViewItem.SubItems
BetterListViewItem.ChildItems
Each of these collections are of type BetterListViewElementCollection<TItem> where TItem is collection element type. All these collection also implement IList<TItem>, ICollection<TItem> and their nongeneric companions.
They also implement extra functionality through IExtendedList<TItem>:
add set of items
remove subset of items
These collections are bound to Better ListView, so any modification to these collection will be projected into control state.
When the collection is created by user code, e.g.:
C#
var myItems = new BetterListViewItemCollection();
Visual Basic
Dim myItems = New BetterListViewItemCollection()
then the collection is not bound to the control and its state is independent on the control's state.
All the collections are both binary and XML serializable.
Specific collections provide several overrides for easy addition of items, for example:
C#
myItems.Add("New Item");
Visual Basic
myItems.Add("New Item")
Adds new item with text 'New Item' in the collection.
All Better ListView collections support adding arbitrary objects, for example:
C#
var person = new Person("Mark Bradley", 13, Gender.Male);
myItems.Add(person);
Visual Basic
Dim person = New Person("Mark Bradley", 13, Gender.Male)
myItems.Add(person)
This will create a new BetterListViewItem with Text property obtained from converting the Person object. The type can either provide custom TypeConverter, or default TypeConverter (for primive types) or ToString method is used for conversion.
Multiple custom items can be added either:
C#
myItems.AddRange(new object[] { person1, person2, "New Person" });
Visual Basic
myItems.AddRange(New Object() {person1, person2, "New Person"})
You can add arbitrary objects to any Better ListView collection. Of course, the objects have to be converted to collection's item type (e.g. BetterListViewItem). If no additional code is provided, the Better ListView will create new item and fill it with text using Object.ToString() method. You can specify this text by providing ToString() method override. Here is a simple Person type providing custom ToString() method:
C#
class Person
{
public string Name;
public int Age;
public override string ToString()
{
return Name;
}
}
Visual Basic
Class Person
Public Name As String
Public Age As Integer
Public Overrides Function ToString() As String
Return Name
End Function
End Class
If you want more control over the conversion, you can provide a custom TypeConverter. The following sample code shows implementation of PersonConverter class that allows conversion from Person type to BetterListViewItem type:
C#
class PersonConverter : TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(BetterListViewItem))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(BetterListViewItem))
{
Person person = (Person)value;
// convert Person instance to BetterListViewItem instance
return new BetterListViewItem(new string[] { person.Name, person.Age.ToString() });
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
Visual Basic
Class PersonConverter
Inherits TypeConverter
Public Overrides Function CanConvertTo(context As ITypeDescriptorContext, destinationType As Type) As Boolean
If destinationType = GetType(BetterListViewItem) Then
Return True
End If
Return MyBase.CanConvertTo(context, destinationType)
End Function
Public Overrides Function ConvertTo(context As ITypeDescriptorContext, culture As CultureInfo, value As Object, destinationType As Type) As Object
If destinationType = GetType(BetterListViewItem) Then
Dim person As Person = DirectCast(value, Person)
' convert Person instance to BetterListViewItem instance
Return New BetterListViewItem(New String() {person.Name, person.Age.ToString()})
End If
Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function
End Class
Once you have the converter implemented, simply mark your custom type with TypeConverterAttribute and Better ListView will make use of the converter instead of just ToString() method:
C#
[TypeConverter(typeof(PersonConverter))]
class Person
{
public string Name;
public int Age;
public override string ToString()
{
return Name;
}
}
Visual Basic
<TypeConverter(GetType(PersonConverter))> _
Class Person
Public Name As String
Public Age As Integer
Public Overrides Function ToString() As String
Return Name
End Function
End Class
Note that TypeConverter is primarily used by Better ListView for conversion to collection's native type or to String. If the TypeConverter does not support conversion to String, the Object.ToString() method is used instead.
Finally, you can add such Person objects to Better ListView item collection and they will get converted automatically:
C#
Person person = new Person();
person.Name = "Jack Black";
person.Age = 38;
listView.Items.Add(person);
Visual Basic
Dim person As New Person()
person.Name = "Jack Black"
person.Age = 38
listView.Items.Add(person)
Other item additon methods like Insert, AddRange or InsertRange can be used as well.
« Check Boxes | Index | Columns » |
Better Thumbnail Browser Documentation | Copyright © 2010-2012 ComponentOwl.com |