?? objectlistview.cs
字號:
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace BrightIdeasSoftware
{
/// <summary>
/// An object list displays 'aspects' of a collection of objects in a listview control.
/// </summary>
/// <remarks>
/// <para>
/// The intelligence for this control is in the columns. OLVColumns are
/// extended so they understand how to fetch an 'aspect' from each row
/// object. They also understand how to sort by their aspect, and
/// how to group them.
/// </para>
/// <para>
/// Aspects are extracted by giving the name of a method to be called or a
/// property to be fetched. These names can be simple names or they can be dotted
/// to chain property access e.g. "Owner.Address.Postcode".
/// Aspects can also be extracted by installing a delegate.
/// </para>
/// <para>
/// Sorting by column clicking and grouping by column are handled automatically.
/// </para>
/// <para>
/// Right clicking on the column header should present a popup menu that allows the user to
/// choose which columns will be visible in the list. This behaviour can be disabled by
/// setting SelectColumnsOnRightClick to false.
/// </para>
/// <para>
/// This list puts sort indicators in the column headers to show the column sorting direction.
/// On Windows XP and later, the system standard images are used.
/// If you wish to replace the standard images with your own images, put entries in the small image list
/// with the key values "sort-indicator-up" and "sort-indicator-down".
/// </para>
/// <para>
/// For these classes to build correctly, the project must have references to these assemblies:
/// <list>
/// <item>System</item>
/// <item>System.Data</item>
/// <item>System.Design</item>
/// <item>System.Drawing</item>
/// <item>System.Windows.Forms (obviously)</item>
/// </list>
/// </para>
/// </remarks>
public partial class ObjectListView : ListView, ISupportInitialize
{
/// <summary>
/// Create an ObjectListView
/// </summary>
public ObjectListView()
: base()
{
this.ColumnClick += new ColumnClickEventHandler(this.HandleColumnClick);
this.Layout += new LayoutEventHandler(this.HandleLayout);
this.ColumnWidthChanging += new ColumnWidthChangingEventHandler(this.HandleColumnWidthChanging);
this.ColumnWidthChanged += new ColumnWidthChangedEventHandler(this.HandleColumnWidthChanged);
base.View = View.Details;
this.DoubleBuffered = true; // kill nasty flickers. hiss... me hates 'em
this.ShowSortIndicators = true;
}
#region Public properties
/// <summary>
/// Get or set all the columns that this control knows about.
/// Only those columns where IsVisible is true will be seen by the user.
/// </summary>
/// <remarks>
/// <para>
/// If you want to add new columns programmatically, add them to
/// AllColumns and then call RebuildColumns(). Normally, you do not have to
/// deal with this property directly. Just use the IDE.
/// </para>
/// <para>If you do add or remove columns from the AllColumns collection,
/// you have to call RebuildColumns() to make those changes take effect.</para>
/// </remarks>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public virtual List<OLVColumn> AllColumns
{
get {
return this.allColumns;
}
set {
if (value == null)
this.allColumns = new List<OLVColumn>();
else
this.allColumns = value;
}
}
private List<OLVColumn> allColumns = new List<OLVColumn>();
/// <summary>
/// If every second row has a background different to the control, what color should it be?
/// </summary>
[Category("Appearance"),
Description("If using alternate colors, what foregroundColor should alterate rows be?"),
DefaultValue(typeof(Color), "")]
public Color AlternateRowBackColor
{
get { return alternateRowBackColor; }
set { alternateRowBackColor = value; }
}
private Color alternateRowBackColor = Color.Empty;
/// <summary>
/// Return the alternate row background color that has been set, or the default color
/// </summary>
[Browsable(false)]
public virtual Color AlternateRowBackColorOrDefault
{
get {
if (alternateRowBackColor == Color.Empty)
return Color.LemonChiffon;
else
return alternateRowBackColor;
}
}
/// <summary>
/// This property forces the ObjectListView to always group items by the given column.
/// </summary>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public virtual OLVColumn AlwaysGroupByColumn
{
get { return alwaysGroupByColumn; }
set { alwaysGroupByColumn = value; }
}
private OLVColumn alwaysGroupByColumn;
/// <summary>
/// If AlwaysGroupByColumn is not null, this property will be used to decide how
/// those groups are sorted. If this property has the value SortOrder.None, then
/// the sort order will toggle according to the users last header click.
/// </summary>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public virtual SortOrder AlwaysGroupBySortOrder
{
get { return alwaysGroupBySortOrder; }
set { alwaysGroupBySortOrder = value; }
}
private SortOrder alwaysGroupBySortOrder = SortOrder.None;
/// <summary>
/// Give access to the image list that is actually being used by the control
/// </summary>
[Browsable(false)]
public virtual ImageList BaseSmallImageList
{
get { return base.SmallImageList; }
}
/// <summary>
/// How does a user indicate that they want to edit cells?
/// </summary>
public enum CellEditActivateMode
{
/// <summary>
/// This list cannot be edited. F2 does nothing.
/// </summary>
None = 0,
/// <summary>
/// A single click on a <strong>subitem</strong> will edit the value. Single clicking the primary column,
/// selects the row just like normal. The user must press F2 to edit the primary column.
/// </summary>
SingleClick = 1,
/// <summary>
/// Double clicking a subitem or the primary column will edit that cell.
/// F2 will edit the primary column.
/// </summary>
DoubleClick = 2,
/// <summary>
/// Pressing F2 is the only way to edit the cells. Once the primary column is being edited,
/// the other cells in the row can be edited by pressing Tab.
/// </summary>
F2Only = 3
}
/// <summary>
/// How does the user indicate that they want to edit a cell?
/// None means that the listview cannot be edited.
/// </summary>
/// <remarks>Columns can also be marked as editable.</remarks>
[Category("Behavior - ObjectListView"),
Description("How does the user indicate that they want to edit a cell?"),
DefaultValue(CellEditActivateMode.None)]
public virtual CellEditActivateMode CellEditActivation
{
get { return cellEditActivation; }
set { cellEditActivation = value; }
}
private CellEditActivateMode cellEditActivation = CellEditActivateMode.None;
/// <summary>
/// Should this list show checkboxes?
/// </summary>
public new bool CheckBoxes
{
get {
return base.CheckBoxes;
}
set {
base.CheckBoxes = value;
// Initialize the state image list so we can display indetermined values.
this.InitializeStateImageList();
}
}
/// <summary>
/// Return the model object of the row that is checked or null if no row is checked
/// or more than one row is checked
/// </summary>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public virtual Object CheckedObject
{
get {
IList checkedObjects = this.CheckedObjects;
if (checkedObjects.Count == 1)
return checkedObjects[0];
else
return null;
}
set {
this.CheckedObjects = new ArrayList(new Object[] { value });
}
}
/// <summary>
/// Get or set the collection of model objects that are checked.
/// When setting this property, any row whose model object isn't
/// in the given collection will be unchecked. Setting to null is
/// equivilent to unchecking all.
/// </summary>
/// <remarks>
/// <para>
/// This property returns a simple collection. Changes made to the returned
/// collection do NOT affect the list. This is different to the behaviour of
/// CheckedIndicies collection.
/// </para>
/// <para>
/// .NET's CheckedItems property is not helpful. It is just a short-hand for
/// iterating through the list looking for items that are checked.
/// </para>
/// <para>
/// The performance of the get method is O(n), where n is the number of items
/// in the control. The performance of the set method is
/// O(n*m) where m is the number of objects being checked. Be careful on long lists.
/// </para>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -