private void add_Click(object sender, RoutedEventArgs e) { IEditableCollectionView editableCollectionView = itemsControl.Items as IEditableCollectionView; if (!editableCollectionView.CanAddNew) { MessageBox.Show("You cannot add items to the list."); return; } // Create a window that prompts the user to enter a new // item to sell. ChangeItemWindow win = new ChangeItemWindow(); //Create a new item to be added to the collection. win.DataContext = editableCollectionView.AddNew(); // If the user submits the new item, commit the new // object to the collection. If the user cancels // adding the new item, discard the new item. if ((bool)win.ShowDialog()) { editableCollectionView.CommitNew(); } else { editableCollectionView.CancelNew(); } }
public void ICVF_Remove() { EntitySet <City> entitySet; EntityCollection <City> entityCollection = this.CreateEntityCollection(out entitySet); IEditableCollectionView view = this.GetIECV(entityCollection); City city = (City)view.AddNew(); city.Name = "Des Moines"; city.CountyName = "King"; city.StateName = "WA"; view.CommitNew(); entityCollection.Add(this.CreateLocalCity("Normandy Park")); entityCollection.Add(this.CreateLocalCity("SeaTac")); // This one was added through the view and will be removed from both view.Remove(city); Assert.IsFalse(entityCollection.Contains(city), "EntityCollection should no longer contain the first entity."); Assert.IsFalse(entitySet.Contains(city), "EntitySet should no longer contain the first entity."); // This one was added directly and will only be removed for the collection city = entityCollection.ElementAt(1); view.Remove(city); Assert.IsFalse(entityCollection.Contains(city), "EntityCollection should no longer contain the entity at index 1."); Assert.IsTrue(entitySet.Contains(city), "EntitySet should still contain the entity at index 1."); }
/// <summary> /// Adds a new item to the collection. /// </summary> /// <returns></returns> Object IEditableCollectionView.AddNew() { IEditableCollectionView iEditableCollectionView = this.currentView as IEditableCollectionView; if (iEditableCollectionView == null) { throw new InvalidOperationException(ExceptionMessage.Format(ExceptionMessages.MemberNotAllowedForView, "AddNew")); } return(iEditableCollectionView.AddNew()); }
// ****************************************************************** /// <summary> /// This virtual method is called when ApplicationCommands.Paste command is executed. /// </summary> /// <param name="target"></param> /// <param name="args"></param> protected virtual void OnExecutedPaste(object sender, ExecutedRoutedEventArgs args) { // parse the clipboard data List <string[]> rowData = ClipboardHelper.ParseClipboardData(); bool hasAddedNewRow = false; // call OnPastingCellClipboardContent for each cell int minRowIndex = Math.Max(Items.IndexOf(CurrentItem), 0); int maxRowIndex = Items.Count - 1; int minColumnDisplayIndex = (SelectionUnit != DataGridSelectionUnit.FullRow) ? Columns.IndexOf(CurrentColumn) : 0; int maxColumnDisplayIndex = Columns.Count - 1; int rowDataIndex = 0; for (int i = minRowIndex; i <= maxRowIndex && rowDataIndex < rowData.Count; i++, rowDataIndex++) { if (CanUserAddRows && i == maxRowIndex) { // add a new row to be pasted to ICollectionView cv = CollectionViewSource.GetDefaultView(Items); IEditableCollectionView iecv = cv as IEditableCollectionView; if (iecv != null) { hasAddedNewRow = true; iecv.AddNew(); if (rowDataIndex + 1 < rowData.Count) { // still has more items to paste, update the maxRowIndex maxRowIndex = Items.Count - 1; } } } else if (i == maxRowIndex) { continue; } int columnDataIndex = 0; for (int j = minColumnDisplayIndex; j < maxColumnDisplayIndex && columnDataIndex < rowData[rowDataIndex].Length; j++, columnDataIndex++) { DataGridColumn column = ColumnFromDisplayIndex(j); string propertyName = ((column as DataGridBoundColumn).Binding as Binding).Path.Path; object item = Items[i]; object value = rowData[rowDataIndex][columnDataIndex]; PropertyInfo pi = item.GetType().GetProperty(propertyName); if (pi != null) { object convertedValue = Convert.ChangeType(value, pi.PropertyType); item.GetType().GetProperty(propertyName).SetValue(item, convertedValue, null); } column.OnPastingCellClipboardContent(item, rowData[rowDataIndex][columnDataIndex]); } } }
/// <summary> /// Add a new item to the underlying collection. Returns the new item. /// After calling AddNew and changing the new item as desired, either /// <seealso cref="IEditableCollectionView.CommitNew"/> or <seealso cref="IEditableCollectionView.CancelNew"/> should be /// called to complete the transaction. /// </summary> object IEditableCollectionView.AddNew() { IEditableCollectionView ecv = ProxiedView as IEditableCollectionView; if (ecv != null) { return(ecv.AddNew()); } else { throw new InvalidOperationException(SR.Get(SRID.MemberNotAllowedForView, "AddNew")); } }
// Token: 0x06007408 RID: 29704 RVA: 0x00212DDC File Offset: 0x00210FDC object IEditableCollectionView.AddNew() { IEditableCollectionView editableCollectionView = this.ProxiedView as IEditableCollectionView; if (editableCollectionView != null) { return(editableCollectionView.AddNew()); } throw new InvalidOperationException(SR.Get("MemberNotAllowedForView", new object[] { "AddNew" })); }
public void ICVF_CancelNew() { EntitySet <City> entitySet = this.CreateEntitySet <City>(); IEditableCollectionView view = this.GetIECV(entitySet); City city = (City)view.AddNew(); Assert.IsTrue(entitySet.Contains(city), "EntitySet should contain the second entity after AddNew."); view.CancelNew(); Assert.IsFalse(entitySet.Contains(city), "EntitySet should no longer contain the second entity after CancelNew."); }
public void ICVF_AddNew() { EntitySet <City> entitySet = this.CreateEntitySet <City>(); IEditableCollectionView view = this.GetIECV(entitySet); City city = (City)view.AddNew(); Assert.IsTrue(entitySet.Contains(city), "EntitySet should contain the first entity after AddNew."); city.Name = "Tukwila"; city.CountyName = "King"; city.StateName = "WA"; view.CommitNew(); Assert.IsTrue(entitySet.Contains(city), "EntitySet should contain the first entity after CommitNew."); }
public object AddNew() { return(_collectionView.AddNew()); }
protected virtual void OnExecutedPaste(ExecutedRoutedEventArgs args) { // Debug.WriteLine("OnExecutedPaste begin"); // parse the clipboard data List <string[]> rowData = new List <string[]>();// ClipboardHelper.ParseClipboardData(); string[] color = { "red", "black", "yellow" }; rowData.Add(color); bool hasAddedNewRow = false; // call OnPastingCellClipboardContent for each cell int minRowIndex = Items.IndexOf(CurrentItem); int maxRowIndex = Items.Count - 1; int minColumnDisplayIndex = (SelectionUnit != DataGridSelectionUnit.FullRow) ? Columns.IndexOf(CurrentColumn) : 0; int maxColumnDisplayIndex = Columns.Count - 1; int rowDataIndex = 0; for (int i = minRowIndex; i <= maxRowIndex && rowDataIndex < rowData.Count; i++, rowDataIndex++) { #region 将数据粘贴至新增的行中 if (CanUserPasteToNewRows && CanUserAddRows && i == maxRowIndex) { // add a new row to be pasted to ICollectionView cv = CollectionViewSource.GetDefaultView(Items); IEditableCollectionView iecv = cv as IEditableCollectionView; if (iecv != null) { hasAddedNewRow = true; iecv.AddNew(); if (rowDataIndex + 1 < rowData.Count) { // still has more items to paste, update the maxRowIndex maxRowIndex = Items.Count - 1; } } } else if (i == maxRowIndex) { continue; } #endregion int columnDataIndex = 0; for (int j = minColumnDisplayIndex; j < maxColumnDisplayIndex && columnDataIndex < rowData[rowDataIndex].Length; j++, columnDataIndex++) { DataGridColumn column = ColumnFromDisplayIndex(j); column.OnPastingCellClipboardContent(Items[i], rowData[rowDataIndex][columnDataIndex]); } } #region 更新选中区域 // update selection if (hasAddedNewRow) { UnselectAll(); UnselectAllCells(); CurrentItem = Items[minRowIndex]; if (SelectionUnit == DataGridSelectionUnit.FullRow) { SelectedItem = Items[minRowIndex]; } else if (SelectionUnit == DataGridSelectionUnit.CellOrRowHeader || SelectionUnit == DataGridSelectionUnit.Cell) { SelectedCells.Add(new DataGridCellInfo(Items[minRowIndex], Columns[minColumnDisplayIndex])); } } #endregion }
/// <summary> /// This virtual method is called when ApplicationCommands.Paste command is executed. /// </summary> /// <param name="args"></param> protected virtual void OnExecutedPaste(object target, ExecutedRoutedEventArgs args) { if (this.ExecutePasteEvent != null) { this.ExecutePasteEvent(target, args); if (args.Handled) { return; } } // parse the clipboard data [row][column] List <string[]> clipboardData = ClipboardHelper.ParseClipboardWithCommaSeparatedValueOrText(); bool hasAddedNewRow = false; Debug.Print(">>> DataGrid Paste: >>>"); #if DEBUG StringBuilder sb = new StringBuilder(); #endif // call OnPastingCellClipboardContent for each cell int minRowIndex = Math.Max(this.Items.IndexOf(this.CurrentItem), 0); int maxRowIndex = this.Items.Count - 1; int minColumnDisplayIndex = (this.SelectionUnit != DataGridSelectionUnit.FullRow) ? this.Columns.IndexOf(this.CurrentColumn) : 0; int maxColumnDisplayIndex = this.Columns.Count - 1; int clipboardRowIndex = 0; for (int i = minRowIndex; i <= maxRowIndex && clipboardRowIndex < clipboardData.Count; i++, clipboardRowIndex++) { if (this.CanUserAddRows && i == maxRowIndex) { // add a new row to be pasted to ICollectionView cv = CollectionViewSource.GetDefaultView(this.Items); IEditableCollectionView iecv = cv as IEditableCollectionView; if (iecv != null) { hasAddedNewRow = true; iecv.AddNew(); if (clipboardRowIndex + 1 < clipboardData.Count) { // still has more items to paste, update the maxRowIndex maxRowIndex = this.Items.Count - 1; } } } else if (i == maxRowIndex) { continue; } int columnDataIndex = 0; for (int j = minColumnDisplayIndex; j < maxColumnDisplayIndex && columnDataIndex < clipboardData[clipboardRowIndex].Length; j++, columnDataIndex++) { DataGridColumn column = this.ColumnFromDisplayIndex(j); object item = this.Items[i]; if (column is DataGridBoundColumn dgbc && dgbc.Binding != null) { string propertyName = (dgbc.Binding as Binding).Path.Path; object value = clipboardData[clipboardRowIndex][columnDataIndex]; PropertyInfo pi = item.GetType().GetProperty(propertyName); if (pi != null) { object convertedValue = Convert.ChangeType(value, pi.PropertyType); item.GetType().GetProperty(propertyName).SetValue(item, convertedValue, null); } }
private void CustomDataGrid_Executed(object sender, ExecutedRoutedEventArgs e) { try { if (e.Command == ApplicationCommands.Paste) { // parse the clipboard data List <string[]> rowData = ClipboardHelper.ParseClipboardData(); bool hasAddedNewRow = false; // call OnPastingCellClipboardContent for each cell int minRowIndex = Math.Max(Items.IndexOf(CurrentItem), 0); int maxRowIndex = Items.Count - 1; int minColumnDisplayIndex = (SelectionUnit != DataGridSelectionUnit.FullRow) ? Columns.IndexOf(CurrentColumn) : 0; int maxColumnDisplayIndex = Columns.Count - 1; int rowDataIndex = 0; for (int i = minRowIndex; i <= maxRowIndex && rowDataIndex < rowData.Count; i++, rowDataIndex++) { if (i == maxRowIndex) { // add a new row to be pasted to ICollectionView cv = CollectionViewSource.GetDefaultView(Items); IEditableCollectionView iecv = cv as IEditableCollectionView; if (iecv != null) { hasAddedNewRow = true; iecv.AddNew(); if (rowDataIndex + 1 < rowData.Count) { // still has more items to paste, update the maxRowIndex maxRowIndex = Items.Count - 1; } } } else if (i == maxRowIndex) { continue; } int columnDataIndex = 0; for (int j = minColumnDisplayIndex; j < maxColumnDisplayIndex && columnDataIndex < rowData[rowDataIndex].Length; j++, columnDataIndex++) { DataGridColumn column = ColumnFromDisplayIndex(j); string propertyName = ((column as DataGridBoundColumn).Binding as Binding).Path.Path; object item = Items[i]; object value = rowData[rowDataIndex][columnDataIndex]; PropertyInfo pi = item.GetType().GetProperty(propertyName); if (pi != null) { try { item.GetType().GetProperty(propertyName).SetValue(item, value, null); } catch { //跳過轉型失敗的欄位值 continue; } } } } } if (e.Command == ApplicationCommands.Copy) { if (this.SelectedItems.Count > 0) { List <string> converttomatrix = new List <string>(); foreach (var row in SelectedItems) { Type data = row.GetType(); var dataobject_column_values = data.GetProperties() .Select(s => string.Format("{0}", s.GetValue(row))) .ToArray(); converttomatrix.Add(string.Join("\t", dataobject_column_values)); } string rawdata = string.Join("\n", converttomatrix.ToArray()); Clipboard.SetText(rawdata); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "錯誤", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly); } }