バージョン

UndeleteRecordsStrategy クラス

DataPresenterBase コントロールにより削除されたレコードを復活させる機能を提供する場合に使用する抽象クラス。
シンタックス
'宣言
 
Public MustInherit Class UndeleteRecordsStrategy 
public abstract class UndeleteRecordsStrategy 
解説

基本のデータ ソースがレコードの削除をサポートする場合、DataPresenter コントロールはユーザー インターフェイスでレコードを削除する機能をエンド ユーザーに適用します。元に戻す機能が有効な場合 (DataPresenterBase.IsUndoEnabled を参照)、エンド ユーザーにこれらのレコードを復活させることができます。この機能を有効にするには、Infragistics.Windows.DataPresenter.Events.RecordsDeletingEventArgs.UndeleteStrategy プロパティを、削除しようとしている Infragistics.Windows.DataPresenter.Events.RecordsDeletingEventArgs.Records の復活を行うことができる UndeleteRecordsStrategy の派生インスタンスに設定する必要があります。

レコードの削除を元に戻す操作を行う場合、Undelete メソッドが呼び出されます。派生されたクラスがこのメソッドを実装します。可能な場合は、削除されたレコードをデータソースに再挿入します。そうでない場合、削除されたレコードの情報と同じ情報がある新しいオブジェクトを作成すると、"元に戻す" アクションを実行します。復活が行われた後、ProcessUndeletedRecords メソッドが呼び出され、非バインドセルの値など、レコードにプロパティを設定できるようになります。

削除されたレコードに子レコードがあり、レコードの削除中に RestoreDescendantActions プロパティが true を戻す場合、DataPresenter は削除されたレコードの子孫に関連付けられた元に戻す/再実行アクションを再接続しようとします。任意のアクションが子孫レコードに関連付けられている場合、ProvideDescendantMapping メソッドが呼び出され、古いレコード情報を新しいデータ項目にマップする手段を提供します。デフォルトでは、子孫レコードの基本データ項目を変更しない場合のみ動作します。

使用例
using Infragistics.Windows;
using Infragistics.Windows.DataPresenter;
using System.Collections;
using System.ComponentModel;
using System.Collections.Specialized;

	private void grid_RecordsDeleting(object sender, Infragistics.Windows.DataPresenter.Events.RecordsDeletingEventArgs e)
	{
		// during the RecordsDeleting the records have not been deleted. in order to 
		// allow the end user to recreate those records, you must provide a custom
		// undeletestrategy that can store the data needed before the delete and 
		// perform the undeletion when requested (when the user attempts to perform
		// an undo). this strategy is a basic one that will reinsert the original
		// item into the containing list so this would work with ibindinglist<t> and 
		// observablecollection<t> type classes. this will not work with datatable/
		// dataset. for more examples please refer to the advanced undo sample in 
		// the feature browser sample
		e.UndeleteStrategy = new ListUndeleteStrategy(e.Records);
	}

	private void grid_Initialized(object sender, EventArgs e)
	{
		DataPresenterBase dp = sender as DataPresenterBase;
		var list = new BindingList<Employee>();
		list.Add(new Employee( "John", new DateTime(2008, 1, 1) ));
		list.Add(new Employee( "Mary", new DateTime(2007, 8, 5) ));
		list.Add(new Employee( "Paul", new DateTime(2003, 3, 14) ));
		dp.DataSource = list;
	}

	#region ListUndeleteStrategy
	// This sample strategy assumes that the objects that were removed from the collection 
	// can be reinserted into the collection and therefore this does not work with things 
	// like dataview, datatable and dataset. you should refer to the advanced undo sample 
	// in the samples browser for more examples
	public class ListUndeleteStrategy : UndeleteRecordsStrategy
	{
		public ListUndeleteStrategy(IList<Record> records)
		{
		}

		public override bool CanUndelete(IList<UndeleteRecordsStrategy.RecordInfo> oldRecords)
		{
			return true;
		}

		public override IDictionary<RecordInfo, object> Undelete(IList<RecordInfo> oldRecords)
		{
			Dictionary<RecordInfo, object> oldToNewMapping = new Dictionary<RecordInfo, object>();

			RecordInfo[] records = oldRecords.ToArray();
			Comparison<RecordInfo> comparison = delegate(RecordInfo item1, RecordInfo item2)
			{
				return item1.DataItemIndex.CompareTo(item2.DataItemIndex);
			};

			// sort by the original index to ensure we get them in the original order
			Utilities.SortMergeGeneric(records, Utilities.CreateComparer(comparison));

			foreach (RecordInfo record in records)
			{
				RecordManager rm = record.RecordManager;
				IList list = rm.SourceItems as IList;

				if (list == null)
					continue;

				int newIndex = Math.Min(list.Count, record.DataItemIndex);
				object newDataItem = record.DataItem;

				list.Insert(newIndex, newDataItem);

				// since we're reusing the same object the old and new will be the same
				oldToNewMapping[record] = newDataItem;
			}

			return oldToNewMapping;
		}
	}
	#endregion //ListUndeleteStrategy

	#region Employee
	public class Employee
	{
		private string _name;
		private DateTime _hireDate;

		public Employee(string name, DateTime hireDate)
		{
			_name = name;
			_hireDate = hireDate;
		}

		public string Name
		{
			get { return _name; }
		}

		public DateTime HireDate
		{
			get { return _hireDate; }
		}
	} 
	#endregion //Employee
xmlns:igDP="http://infragistics.com/DataPresenter"

<igDP:XamDataGrid xmlns:igDP="http://infragistics.com/DataPresenter"
    
x:Name="grid" 
    
BindToSampleData="True"
    
IsUndoEnabled="True"
    
Initialized="grid_Initialized"
    
RecordsDeleting="grid_RecordsDeleting">
</igDP:XamDataGrid>
Imports Infragistics.Windows
Imports Infragistics.Windows.DataPresenter
Imports Infragistics.Windows.DataPresenter.UndeleteRecordsStrategy
Imports System.Globalization
Imports System.ComponentModel

    Private Sub grid_RecordsDeleting(ByVal sender As System.Object, ByVal e As Infragistics.Windows.DataPresenter.Events.RecordsDeletingEventArgs)
        ' during the RecordsDeleting the records have not been deleted. in order to 
        ' allow the end user to recreate those records, you must provide a custom
        ' undeletestrategy that can store the data needed before the delete and 
        ' perform the undeletion when requested (when the user attempts to perform
        ' an undo). this strategy is a basic one that will reinsert the original
        ' item into the containing list so this would work with ibindinglist<t> and 
        ' observablecollection<t> type classes. this will not work with datatable/
        ' dataset. for more examples please refer to the advanced undo sample in 
        ' the feature browser sample
        e.UndeleteStrategy = New ListUndeleteStrategy(e.Records)
    End Sub

    Private Sub grid_Initialized(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim dp As DataPresenterBase = DirectCast(sender, DataPresenterBase)
        Dim list As BindingList(Of Employee) = New BindingList(Of Employee)()
        list.Add(New Employee("John", New DateTime(2008, 1, 1)))
        list.Add(New Employee("Mary", New DateTime(2007, 8, 5)))
        list.Add(New Employee("Paul", New DateTime(2003, 3, 14)))
        dp.DataSource = list
    End Sub


#Region "ListUndeleteStrategy"
    ' This sample strategy assumes that the objects that were removed from the collection 
    ' can be reinserted into the collection and therefore this does not work with things 
    ' like dataview, datatable and dataset. you should refer to the advanced undo sample 
    ' in the samples browser for more examples
    Public Class ListUndeleteStrategy
        Inherits UndeleteRecordsStrategy
        Public Sub New(ByVal records As IList(Of Record))
        End Sub

        Public Overrides Function CanUndelete(ByVal oldRecords As IList(Of UndeleteRecordsStrategy.RecordInfo)) As Boolean
            Return True
        End Function

        Public Overrides Function Undelete(ByVal oldRecords As IList(Of RecordInfo)) As IDictionary(Of RecordInfo, Object)
            Dim oldToNewMapping As New Dictionary(Of RecordInfo, Object)
            Dim records As RecordInfo() = oldRecords.ToArray()

            Dim comparison As Comparison(Of RecordInfo) = Function(item1 As RecordInfo, item2 As RecordInfo) item1.DataItemIndex.CompareTo(item2.DataItemIndex)

            ' sort by the original index to ensure we get them in the original order
            Utilities.SortMergeGeneric(records, Utilities.CreateComparer(comparison))

            For Each record As RecordInfo In records
                Dim rm As RecordManager = record.RecordManager
                Dim list As IList = TryCast(rm.SourceItems, IList)

                If list Is Nothing Then Continue For

                Dim newIndex As Integer = Math.Min(list.Count, record.DataItemIndex)
                Dim newDataItem As Object = record.DataItem

                list.Insert(newIndex, newDataItem)

                ' since we're reusing the same object the old and new will be the same
                oldToNewMapping(record) = newDataItem
            Next record

            Return oldToNewMapping
        End Function
    End Class
#End Region 'ListUndeleteStrategy

#Region "Employee"
    Public Class Employee
        Private _name As String
        Private _hireDate As DateTime

        Public Sub New(ByVal name As String, ByVal hireDate As DateTime)
            _name = name
            _hireDate = hireDate
        End Sub

        Public ReadOnly Property Name() As String
            Get
                Return _name
            End Get
        End Property

        Public ReadOnly Property HireDate() As DateTime
            Get
                Return _hireDate
            End Get
        End Property
    End Class
#End Region 'Employee
参照