È possibile ottenere ciò rendendo modificabile la riga desiderata e utilizzare l'evento CellEditActivation. Inizializzare l'OLV e "delete-colonna" come segue:
// fire cell edit event on single click
objectListView1.CellEditActivation = ObjectListView.CellEditActivateMode.SingleClick;
objectListView1.CellEditStarting += ObjectListView1OnCellEditStarting;
// enable cell edit and always set cell text to "Delete"
deleteColumn.IsEditable = true;
deleteColumn.AspectGetter = delegate {
return "Delete";
};
Quindi è possibile rimuovere la riga nel gestore CellEditStarting non appena la colonna si fa clic:
private void ObjectListView1OnCellEditStarting(object sender, CellEditEventArgs e) {
// special cell edit handling for our delete-row
if (e.Column == deleteColumn) {
e.Cancel = true; // we don't want to edit anything
objectListView1.RemoveObject(e.RowObject); // remove object
}
}
Per migliorare su questo, voi può visualizzare un'immagine oltre al testo.
// assign an ImageList containing at least one image to SmallImageList
objectListView1.SmallImageList = imageList1;
// always display image from index 0 as default image for deleteColumn
deleteColumn.ImageGetter = delegate {
return 0;
};
Risultato:

Se non si desidera visualizzare qualsiasi testo accanto all'immagine, è possibile utilizzare
deleteColumn.AspectToStringConverter = delegate {
return String.Empty;
};
Si potrebbe anche impostare l'aspetto di un stringa vuota, ma considerala come "best practice". Restituendo ancora un aspetto, l'ordinamento e il raggruppamento continueranno a funzionare.
fonte
2012-11-02 08:31:46