/// <summary> /// Переводит ячейку в режим редвктирования, предоставляет контрол режима редактирования. Возвращает успех редактирования /// </summary> /// <param name="cell"></param> /// <param name="editControl">Контрол режима редактирования (из GetCellContent())</param> public static bool Edit(this DataGridCell cell, Predicate <FrameworkElement> editControl) { if (!cell.IsReadOnly) { try { cell.IsEditing = true; if (cell.IsEditing) { var control = cell.Column.GetCellContent(cell.DataContext); cell.UpdateLayout(); // обновление значений control return(editControl(control)); } } finally { cell.IsEditing = false; } } return(false); }
// Here the datagrid itself is parsed because it is the datagrid cells that // need to be marked bad. // Returns true if there are issues. private bool ValidateRenamedList() { // OMG!! bool ThereAreIssues = false; bool ThereAreIllegals = false; int invalidCount = 0; int changedCount = 0; DataGrid dgr = dg_names; List <string> NotUniqueOnDiskList = new List <string>(); List <string> NotUniqueInThisList = new List <string>(); string basePath = LocfRenamer.PathPart; int numRows = LocfRenamer.TheFiles.Count(); for (int i = 0; i < numRows; i++) { DataGridRow thisRow = WPFDataGridHelpers.GetRow(dgr, i); DataGridCell CellCur = dgr.GetCell(thisRow, 0); DataGridCell CellRen = dgr.GetCell(thisRow, 1); // But the cell reads "" if it is not first updated when this validate method is called from // a textchanged event!!! CellRen.UpdateLayout(); if (CellRen != null) { // readonly cells are TextBlocks!! TextBlock tblkCur = CellCur.Content as TextBlock; // But when not editing a textblock?? //TextBox tbxRen = CellRen.Content as TextBox; TextBlock tblkRen = CellRen.Content as TextBlock; if (tblkRen != null) { string name_grid_cell_Fnew = tblkRen.Text; string name_grid_cell_Fcur = tblkCur.Text; string msg = string.Empty; string fullPath = basePath + name_grid_cell_Fnew; if (!Helpers.IsValidWindowsFileName(name_grid_cell_Fnew)) { // invalid characters Dispatcher.BeginInvoke((Action)(() => CellRen.Foreground = Brushes.Red)); ThereAreIssues = true; ThereAreIllegals = true; invalidCount++; continue; } else { if (!name_grid_cell_Fcur.Equals(name_grid_cell_Fnew, StringComparison.CurrentCultureIgnoreCase)) { // new tlbk <> cur tlbk There is a change, no matter what. changedCount++; Dispatcher.BeginInvoke((Action)(() => CellRen.Foreground = Brushes.DarkGreen)); // innocent until proven guilty if (!Helpers.MarkDataGridCellForFile(CellRen, fullPath, false)) { // There is another file on disk like this one. NotUniqueOnDiskList.Add(name_grid_cell_Fnew); Dispatcher.BeginInvoke((Action)(() => CellRen.Foreground = Brushes.Red)); ThereAreIssues = true; invalidCount++; } else { // No file issue, now look for duplicates in the bound list. // We are comparing this row to all entries // in the bound list looking for the same name used for other current names. foreach (RenamerFileName list_rfn in LocfRenamer.TheFiles) { if (list_rfn.Present_Name.Equals(name_grid_cell_Fcur, StringComparison.CurrentCultureIgnoreCase)) { // This must be the same name. Do nothing. Go on to the next. continue; // We do not want to do any more comparisons. } if (list_rfn.Changed_Name.Equals(name_grid_cell_Fnew, StringComparison.CurrentCultureIgnoreCase)) { // list changed = a new changed, must be a name already used. // In this code the same name can wind up multiple times in the list if this // is not checked first. if (!NotUniqueInThisList.Contains(name_grid_cell_Fnew)) { NotUniqueInThisList.Add(name_grid_cell_Fnew); } // this item has an issue ThereAreIssues = true; Dispatcher.BeginInvoke((Action)(() => CellRen.Foreground = Brushes.Red)); invalidCount++; // When the name is a duplicate within the list, the prior duplicates will have // been marked green as valid ne ones. We need to pass through the list again to remark it to red for (int j = 0; j < numRows; j++) { DataGridRow thisReRow = WPFDataGridHelpers.GetRow(dgr, j); DataGridCell CellReRen = dgr.GetCell(thisReRow, 1); TextBlock retblkRen = CellReRen.Content as TextBlock; if (retblkRen.Text.Equals(name_grid_cell_Fnew)) { Dispatcher.BeginInvoke((Action)(() => CellReRen.Foreground = Brushes.Red)); } } } } } } else // no change at all { // item not changed Dispatcher.BeginInvoke((Action)(() => CellRen.Foreground = Brushes.Black)); } } //// If after the change the two wind up being equal, like in the case of a reset, then make it black. //if (name_grid_cell_Fcur.Equals(name_grid_cell_Fcur)) //{ // Dispatcher.BeginInvoke((Action)(() => CellRen.Foreground = Brushes.Black)); //} } } } if (ThereAreIssues) { string msgDetail = string.Empty; string moreDetail = string.Empty; if (ThereAreIllegals) { msgDetail = "There are illegal characters."; } if (NotUniqueOnDiskList.Count > 0) { moreDetail = "There are existing files elsewhere: " + string.Join(" , ", NotUniqueOnDiskList); if (ThereAreIllegals) { msgDetail = msgDetail + " , " + moreDetail; } else { msgDetail = moreDetail; } } if (NotUniqueInThisList.Count > 0) { moreDetail = "These are duplicates names: " + string.Join(" , ", NotUniqueInThisList); if (ThereAreIllegals || NotUniqueOnDiskList.Count > 0) { msgDetail = msgDetail + " , " + moreDetail; } else { msgDetail = moreDetail; } } Dispatcher.BeginInvoke((Action)(() => { Lb_topmsg.Content = "There are naming issues."; Lb_topmsg.Foreground = Brushes.Red; TBLK_msg.Text = msgDetail; TBLK_msg.Visibility = Visibility.Visible; TBLK_msg.Foreground = Brushes.Red; })); } else { Dispatcher.BeginInvoke((Action)(() => { Lb_topmsg.Content = string.Empty; Lb_topmsg.Foreground = Brushes.Black; TBLK_msg.Text = string.Empty; TBLK_msg.Foreground = Brushes.Black; TBLK_msg.Visibility = Visibility.Collapsed; })); } Dispatcher.BeginInvoke((Action)(() => { But_rename.IsEnabled = (changedCount > invalidCount); })); return(ThereAreIssues); }