CellEditor.cs
Upload User: linjidu
Upload Date: 2021-04-29
Package Size: 49798k
Code Size: 31k
Category:

CSharp

Development Platform:

C#

  1. #region Copyright (c) 2000-2009 Developer Express Inc.
  2. /*
  3. {*******************************************************************}
  4. {                                                                   }
  5. {       Developer Express .NET Component Library                    }
  6. {       DXGrid                                 }
  7. {                                                                   }
  8. {       Copyright (c) 2000-2009 Developer Express Inc.              }
  9. {       ALL RIGHTS RESERVED                                         }
  10. {                                                                   }
  11. {   The entire contents of this file is protected by U.S. and       }
  12. {   International Copyright Laws. Unauthorized reproduction,        }
  13. {   reverse-engineering, and distribution of all or any portion of  }
  14. {   the code contained in this file is strictly prohibited and may  }
  15. {   result in severe civil and criminal penalties and will be       }
  16. {   prosecuted to the maximum extent possible under the law.        }
  17. {                                                                   }
  18. {   RESTRICTIONS                                                    }
  19. {                                                                   }
  20. {   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES           }
  21. {   ARE CONFIDENTIAL AND PROPRIETARY TRADE                          }
  22. {   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS   }
  23. {   LICENSED TO DISTRIBUTE THE PRODUCT AND ALL ACCOMPANYING .NET    }
  24. {   CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY.                 }
  25. {                                                                   }
  26. {   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      }
  27. {   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        }
  28. {   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       }
  29. {   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  }
  30. {   AND PERMISSION FROM DEVELOPER EXPRESS INC.                      }
  31. {                                                                   }
  32. {   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON       }
  33. {   ADDITIONAL RESTRICTIONS.                                        }
  34. {                                                                   }
  35. {*******************************************************************}
  36. */
  37. #endregion Copyright (c) 2000-2009 Developer Express Inc.
  38. using System.Windows;
  39. using DevExpress.Wpf.Core;
  40. using System.Windows.Media;
  41. using System.ComponentModel;
  42. using System.Windows.Input;
  43. using System.Globalization;
  44. using System.Windows.Controls;
  45. using System;
  46. using DevExpress.Wpf.Editors;
  47. using System.Windows.Data;
  48. using DevExpress.Wpf.Core.Native;
  49. using DevExpress.XtraEditors.DXErrorProvider;
  50. using DevExpress.Wpf.Editors.Helpers;
  51. using DevExpress.Wpf.Editors.Settings;
  52. using DevExpress.Wpf.Editors.Validation;
  53. using DevExpress.Wpf.Utils.Themes;
  54. using DevExpress.Wpf.Data;
  55. using System.Collections.Generic;
  56. using System.Collections;
  57. using DevExpress.Wpf.Editors.Native;
  58. namespace DevExpress.Wpf.Grid {
  59. public abstract class CellEditorBase : DataContentPresenter, IWeakEventListener, IDisplayTextProvider {
  60. protected enum BaseEditSourceType { EditSettings, CellTemplate }
  61. const int EscCode = 27;
  62. public static readonly DependencyProperty FieldNameProperty;
  63. public static readonly DependencyProperty ColumnProperty;
  64. static CellEditorBase() {
  65. FocusableProperty.OverrideMetadata(typeof(CellEditorBase), new FrameworkPropertyMetadata(true));
  66. FieldNameProperty = DependencyProperty.Register("FieldName", typeof(string), typeof(CellEditorBase), new FrameworkPropertyMetadata(null, (d, e) => ((CellEditorBase)d).OnFieldNameChanged()));
  67. ColumnProperty = DependencyProperty.Register("Column", typeof(GridColumn), typeof(CellEditorBase), new FrameworkPropertyMetadata(null, (d, e) => ((CellEditorBase)d).OnColumnChanged((GridColumn)e.OldValue)));
  68. TableView.IsFocusedCellProperty.OverrideMetadata(typeof(CellEditorBase), new FrameworkPropertyMetadata((d, e) => ((CellEditorBase)d).OnIsFocusedCellChanged()));
  69. GridColumn.NavigationIndexProperty.AddOwner(typeof(CellEditorBase));
  70. }
  71. static bool GetIsEditorButtonVisible(EditorButtonShowMode editorShowMode, bool isFocusedCell, bool isFocusedRow, bool isEditorVisible) {
  72. switch(editorShowMode) {
  73. case EditorButtonShowMode.ShowAlways:
  74. return true;
  75. case EditorButtonShowMode.ShowOnlyInEditor:
  76. return isEditorVisible;
  77. case EditorButtonShowMode.ShowForFocusedRow:
  78. return isFocusedRow;
  79. default:
  80. return isFocusedCell;
  81. }
  82. }
  83. public string FieldName {
  84. get { return (string)GetValue(FieldNameProperty); }
  85. set { SetValue(FieldNameProperty, value); }
  86. }
  87. public GridColumn Column {
  88. get { return (GridColumn)GetValue(ColumnProperty); }
  89. set { SetValue(ColumnProperty, value); }
  90. }
  91. public int NavigationIndex {
  92. get { return (int)GetValue(GridColumn.NavigationIndexProperty); }
  93. set { SetValue(GridColumn.NavigationIndexProperty, value); }
  94. }
  95. public bool IsEditorVisible { get { return Edit.EditMode == EditMode.InplaceActive; } }
  96. internal void LockEditorFocus() { Edit.LockEditorFocus(); }
  97. internal void UnlockEditorFocus() { Edit.UnlockEditorFocus(); }
  98. protected BaseEditSourceType EditorSourceType { get; private set; }
  99. protected BaseEdit editCore;
  100. IBaseEdit editWrapper;
  101. EditGridCellData cellData;
  102. internal virtual bool IsInTree { get { return View != null; } }
  103. protected virtual bool IsRowFocused { get { return RowHandle == View.FocusedRowHandle; } }
  104. protected bool HasAccessToCellValue { get { return IsInTree && Column != null; } }
  105. internal object EditableValue { get { return Edit.EditValue; } set { Edit.EditValue = value; } }
  106. internal GridViewBase View { get { return GridControl.GetActiveView(this); } }
  107. protected GridControl Grid { get { return View.Grid; } }
  108. protected abstract int RowHandle { get; }
  109. protected abstract bool IsReadOnly { get; }
  110. internal RowData RowData { get { return RowData.GetRowData(this); } }
  111. protected abstract bool OverrideCellTemplate { get; }
  112. protected string FieldNameCore { get { return Column.FieldName; } }
  113. protected IBaseEdit Edit { get { return editWrapper != null ? editWrapper : FakeBaseEdit.Instance; } }
  114. protected virtual EditGridCellData CellData {
  115. get { return cellData; }
  116. set {
  117. if(CellData == value)
  118. return;
  119. if(CellData != null) {
  120. CellData.ContentChanged -= new EventHandler(OnInnerContentChanged);
  121. if(CellData.CellEditor == this)
  122. CellData.CellEditor = null;
  123. }
  124. cellData = value;
  125. if(CellData != null) {
  126. CellData.ContentChanged += new EventHandler(OnInnerContentChanged);
  127. CellData.CellEditor = this;
  128. }
  129. }
  130. }
  131. protected internal bool IsChildElement(IInputElement element) {
  132. return Edit.IsChildElement(element);
  133. }
  134. void SetEdit(BaseEdit value) {
  135. if(editCore == value)
  136. return;
  137. if(editCore != null) {
  138. editCore.EditorActivated -= new RoutedEventHandler(OnEditorActivated);
  139. editCore.PreviewLostKeyboardFocus -= new KeyboardFocusChangedEventHandler(OnEditorPreviewLostKeyboardFocus);
  140. }
  141. editCore = value;
  142. editWrapper = editCore != null ? new BaseEditWrapper(editCore) : null;
  143. if(editCore != null) {
  144. editCore.EditorActivated += new RoutedEventHandler(OnEditorActivated);
  145. editCore.PreviewLostKeyboardFocus += new KeyboardFocusChangedEventHandler(OnEditorPreviewLostKeyboardFocus);
  146. }
  147. }
  148. bool IsCellFocused { 
  149. get {
  150. return TableView.GetIsFocusedCell(this); 
  151. }
  152. protected virtual void OnColumnContentChanged(object sender, ColumnContentChangedEventArgs e) {
  153. if(e.Property == GridColumn.DisplayTemplateProperty) {
  154. UpdateDisplayTemplate();
  155. return;
  156. }
  157. if(e.Property == null)
  158. UpdateContent();
  159. }
  160. internal Locker SetKeyboardFocusToEditorLocker { get { return View.SetKeyboardFocusToEditorLocker; } }
  161. protected CellEditorBase() {
  162. }
  163. protected virtual void UpdateEditContext() {
  164. if(HasAccessToCellValue)
  165. EditableValue = Grid.GetCellValue(RowHandle, FieldNameCore);
  166. }
  167. internal void ActivateOnLeftMouseButton(MouseButtonEventArgs e, bool isMouseDown) {
  168. if(IsCellFocused) {
  169. if(!Edit.IsEditorActive) {
  170. ShowEditorIfNotVisible(!isMouseDown);
  171. if(IsEditorVisible && isMouseDown) {
  172. View.DataPresenter.EnqueueImmediateAction(new RaisePostponedMouseEventAction(this, e));
  173. e.Handled = true;
  174. }
  175. }
  176. }
  177. }
  178. protected override void OnPreviewTextInput(TextCompositionEventArgs e) {
  179. base.OnPreviewTextInput(e);
  180. if(string.IsNullOrEmpty(e.Text) || !string.IsNullOrEmpty(e.ControlText) || !string.IsNullOrEmpty(e.SystemText) || (int)e.Text[0] == EscCode)
  181. return;
  182. if(!IsEditorVisible)
  183. ShowEditorAndSelectAll();
  184. if(IsEditorVisible && !Edit.IsEditorActive) {
  185. View.DataPresenter.EnqueueImmediateAction(new RaisePostponedTextInputEventAction(this, e));
  186. e.Handled = true;
  187. }
  188. }
  189. internal void ProcessActivatingKey(KeyEventArgs e) {
  190. Edit.ProcessActivatingKey(e);
  191. }
  192. protected override void OnPreviewKeyDown(KeyEventArgs e) {
  193. base.OnPreviewKeyDown(e);
  194. if(!IsEditorVisible && Edit.IsActivatingKey(e)) {
  195. ShowEditorAndSelectAll();
  196. if(IsEditorVisible) {
  197. View.DataPresenter.EnqueueImmediateAction(new ProcessActivatingKeyAction(this, e));
  198. e.Handled = true;
  199. } else {
  200. RaiseKeyDownEvent(e);
  201. }
  202. return;
  203. }
  204. if(!Edit.NeedsKey(e)) {
  205. if(e.Key == Key.Enter) {
  206. if(IsEditorVisible) {
  207. CommitEditor();
  208. View.EditorWasClosed = true;
  209. } else {
  210. ShowEditorAndSelectAll();
  211. }
  212. e.Handled = true;
  213. }
  214. if(e.Key == Key.F2) {
  215. if(!IsEditorVisible) {
  216. ShowEditorAndSelectAll();
  217. e.Handled = true;
  218. }
  219. }
  220. if(e.Key == Key.Escape) {
  221. if(IsEditorVisible) {
  222. CancelEditInVisibleEditor();
  223. e.Handled = true;
  224. }
  225. else
  226. CancelRowEdit();
  227. }
  228. if(!e.Handled) {
  229. RaiseKeyDownEvent(e);
  230. }
  231. }
  232. }
  233. internal void CancelEditInVisibleEditor() {
  234. if(!IsEditorVisible)
  235. return;
  236. RestoreValidationError();
  237. HideEditor();
  238. View.EditorWasClosed = true;
  239. }
  240. protected virtual void CancelRowEdit() {
  241. }
  242. protected virtual void RestoreValidationError() { 
  243. }
  244. void RaiseKeyDownEvent(KeyEventArgs e) {
  245. e.Handled = true;
  246. View.ProcessKeyDown(e);
  247. }
  248. internal void ShowEditor() {
  249. ShowEditor(false);
  250. }
  251. internal void ShowEditor(bool selectAll) {
  252. if(!CanShowEditor())
  253. return;
  254. if(!IsEditorVisible) {
  255. UpdateEditTemplate();
  256. Edit.IsReadOnly = IsReadOnly;
  257. Edit.EditMode = EditMode.InplaceActive;
  258. View.SetActiveEditor(editCore);
  259. Edit.SetKeyboardFocus();
  260. UpdateEditContext();
  261. Edit.EditValueChanged += new EditValueChangedEventHandler(OnEditValueChanged);
  262. }
  263. View.OnShowEditor();
  264. View.EditorWasClosed = false;
  265. UpdateEditorButtonVisibility();
  266. Edit.IsValueChanged = false;
  267. if(selectAll)
  268. View.DataPresenter.EnqueueImmediateAction(new SelectAllAction(this));
  269. }
  270. void OnEditValueChanged(object sender, EditValueChangedEventArgs e) {
  271. View.RaiseCellValueChanging(new CellValueChangedEventArgs(View, RowHandle, Column, Edit.EditValue, e.OldValue) { RoutedEvent = TableView.CellValueChangingEvent });
  272. OnEditValueChanged();
  273. }
  274. protected virtual void OnEditValueChanged() {
  275. }
  276. internal void ShowEditorIfNotVisible(bool selectAll) {
  277. if(!IsEditorVisible)
  278. ShowEditor(selectAll);
  279. }
  280. internal void ShowEditorAndSelectAll() {
  281. ShowEditor(true);
  282. }
  283. protected internal virtual bool CanShowEditor() {
  284. return IsCellFocused && RaiseShowingEditor();
  285. }
  286. bool RaiseShowingEditor() {
  287. ShowingEditorEventArgs e = new ShowingEditorEventArgs(View, RowHandle, Column);
  288. View.RaiseShowingEditor(e);
  289. return !e.Cancel;
  290. }
  291. internal void CommitEditor() {
  292. if(!IsEditorVisible)
  293. return;
  294. if(PostEditor())
  295. HideEditor();
  296. }
  297. protected internal virtual bool PostEditor() {
  298. return true;
  299. }
  300. protected internal virtual void ValidateEditor() {
  301. }
  302. internal void HideEditor() {
  303. if(IsEditorVisible) {
  304. Edit.EditMode = EditMode.InplaceInactive;
  305. Edit.EditValueChanged -= new EditValueChangedEventHandler(OnEditValueChanged);
  306. if(View.ActiveEditor == editCore)
  307. View.SetActiveEditor(null);
  308. OnHiddenEditor();
  309. }
  310. }
  311. protected virtual void OnHiddenEditor() {
  312. UpdateEditorButtonVisibility();
  313. View.OnHideEditor();
  314. View.RaiseHiddenEditor(new EditorEventArgs(View, RowHandle, Column, editCore) { RoutedEvent = TableView.HiddenEditorEvent });
  315. }
  316. void OnIsFocusedCellChanged() {
  317. if(IsCellFocused) {
  318. if(View.IsKeyboardFocusWithin) {
  319. Edit.SetKeyboardFocus();
  320. if(!IsKeyboardFocusWithin)
  321. Keyboard.Focus(this);
  322. }
  323. UpdateEditorToShow();
  324. }
  325. else {
  326. if(IsEditorVisible)
  327. CommitEditor();
  328. if(IsInTree) {
  329. if(IsKeyboardFocusWithin)
  330. Keyboard.Focus(View);
  331. ClearViewCurrentCellEditor(View);
  332. }
  333. }
  334. UpdateEditorButtonVisibility();
  335. }
  336. protected void ClearViewCurrentCellEditor(GridViewBase view) {
  337. if(view.CurrentCellEditor == this)
  338. view.CurrentCellEditor = null;
  339. }
  340. void UpdateEditorToShow() {
  341. if(IsCellFocused) {
  342. View.CurrentCellEditor = this;
  343. }
  344. }
  345. #if DEBUGTEST
  346. internal int InnerContentChangedFireCount { get; set; }
  347. #endif
  348. protected override void OnInnerContentChangedCore() {
  349. base.OnInnerContentChangedCore();
  350. #if DEBUGTEST            
  351. InnerContentChangedFireCount++;
  352. #endif
  353. if(!IsInTree)
  354. return;
  355. UpdateContent();
  356. UpdateEditorButtonVisibility();
  357. UpdateValidationError();
  358. }
  359. void OnColumnChanged(GridColumn oldValue) {
  360. UpdateData();
  361. if(oldValue != null) {
  362. ContentChangedEventManager.RemoveListener(oldValue, this);
  363. }
  364. if(Column != null) {
  365. ContentChangedEventManager.AddListener(Column, this);
  366. UpdateContent();
  367. }
  368. }
  369. void OnFieldNameChanged() {
  370. if(Column == null && FieldName != null)
  371. Column = View.Columns[FieldName];
  372. }
  373. protected virtual void UpdateData() {
  374. if(HasAccessToCellValue) {
  375. CellData = (EditGridCellData)RowData.GetCellDataByColumn(Column);
  376. } else {
  377. CellData = null;
  378. Content = null;
  379. }
  380. }
  381. protected void UpdateContent() {
  382. if(!IsInTree)
  383. return;
  384. if(Column.ActualCellTemplateSelector.SelectTemplate(CellData, this) == null || OverrideCellTemplate) {
  385. if(editCore == null || !IsProperEditorSettings()) {
  386. BaseEdit newEdit = CreateEditor(Column.ActualEditSettings);
  387. newEdit.DataContext = CellData;
  388. InitializeBaseEdit(newEdit, BaseEditSourceType.EditSettings);
  389. ContentTemplateSelector = null;
  390. Content = newEdit;
  391. } else {
  392. if(!object.ReferenceEquals(BaseEditHelper.GetEditSettings(editCore), Column.ActualEditSettings)) {
  393. Column.ActualEditSettings.ApplyToEdit(editCore, true, Column);
  394. editCore.DataContext = CellData;
  395. }
  396. }
  397. } else {
  398. Content = CellData;
  399. ContentTemplateSelector = Column.ActualCellTemplateSelector;
  400. if(EditorSourceType == BaseEditSourceType.EditSettings)
  401. SetEdit(null);
  402. }
  403. }
  404. protected virtual bool IsProperEditorSettings() {
  405. return object.ReferenceEquals(BaseEditHelper.GetEditSettings(editCore), Column.ActualEditSettings);
  406. }
  407. protected virtual BaseEdit CreateEditor(BaseEditSettings settings){
  408. return settings.CreateEditor(Column);
  409. }
  410. public override void OnApplyTemplate() {
  411. base.OnApplyTemplate();
  412. BaseEdit editFromTemplate = GetTemplateChild("PART_Editor") as BaseEdit;
  413. if(editFromTemplate != null)
  414. InitializeBaseEdit(editFromTemplate, BaseEditSourceType.CellTemplate);
  415. }
  416. void InitializeBaseEdit(BaseEdit newEdit, BaseEditSourceType newBaseEditSourceType) {
  417. this.EditorSourceType = newBaseEditSourceType;
  418. newEdit.EditMode = EditMode.InplaceInactive;
  419. UpdateEditValue(newEdit);
  420. SetEdit(newEdit);
  421. UpdateEditorButtonVisibility();
  422. UpdateDisplayTemplate();
  423. UpdateValidationError();
  424. SetDisplayTextProvider(newEdit);
  425. }
  426. protected virtual void SetDisplayTextProvider(BaseEdit newEdit) {
  427. BaseEditHelper.SetDisplayTextProvider(newEdit, this);
  428. }
  429. protected virtual void UpdateEditValue(BaseEdit editor) {
  430. editor.SetBinding(BaseEdit.EditValueProperty, new Binding(GridCellData.ValueProperty.Name) { Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
  431. }
  432. protected override HitTestResult HitTestCore(PointHitTestParameters hitTestParameters) {
  433. HitTestResult result = base.HitTestCore(hitTestParameters);
  434. return result == null ? new PointHitTestResult(this, hitTestParameters.HitPoint) : result;
  435. }
  436. void OnEditorPreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) {
  437. if(!IsInTree || Edit.IsChildElement(e.NewFocus)) {
  438. return;
  439. }
  440. if(IsEditorVisible && Edit.IsEditorActive) {
  441. SetKeyboardFocusToEditorLocker.DoLockedAction(() => CommitEditor());
  442. }
  443. if(View.HasCellEditorError)
  444. e.Handled = true;
  445. }
  446. void OnEditorActivated(object sender, RoutedEventArgs e) {
  447. View.RaiseShownEditor(new EditorEventArgs(View, RowHandle, Column, editCore));
  448. }
  449. internal void UpdateEditorButtonVisibility() {
  450. if(!IsInTree)
  451. return;
  452. Edit.IsEditButtonsVisible = GetIsEditorButtonVisible(View.EditorButtonShowMode, TableView.GetIsFocusedCell(this), IsRowFocused, IsEditorVisible);
  453. }
  454. protected virtual void UpdateEditTemplate() {
  455. Edit.SetEditTemplate(Column.EditTemplate);
  456. }
  457. protected virtual void UpdateDisplayTemplate() {
  458. Edit.SetDisplayTemplate(Column.DisplayTemplate);
  459. }
  460. protected virtual void UpdateValidationError() {
  461. }
  462. internal void SelectAll() {
  463. Edit.SelectAll();
  464. }
  465. #region IWeakEventListener Members
  466. bool IWeakEventListener.ReceiveWeakEvent(Type managerType, object sender, EventArgs e) {
  467. if(managerType == typeof(ContentChangedEventManager)) {
  468. OnColumnContentChanged(sender, (ColumnContentChangedEventArgs)e);
  469. return true;
  470. }
  471. return false;
  472. }
  473. #endregion
  474. #region IDisplayTextProvider Members
  475. string IDisplayTextProvider.GetDisplayText(string originalDisplayText) {
  476. if(!IsInTree)
  477. return originalDisplayText;
  478. return View.Grid.RaiseCustomDisplayText(RowHandle, Column, Edit.EditValue, originalDisplayText);
  479. }
  480. #endregion
  481. }
  482. public class CellEditor : CellEditorBase {
  483. static CellEditor() {
  484. DefaultStyleKeyProperty.OverrideMetadata(typeof(CellEditor), new FrameworkPropertyMetadata(typeof(CellEditor)));
  485. RowData.RowDataProperty.OverrideMetadata(typeof(CellEditor), new FrameworkPropertyMetadata((d, e) => ((CellEditor)d).OnRowDataChanged((RowData)e.OldValue)));
  486. }
  487. public CellEditor() {
  488. }
  489. internal override bool IsInTree { get { return base.IsInTree && RowData != null; } }
  490. protected override int RowHandle { get { return RowData.RowHandle != null ? RowData.RowHandle.Value : GridControl.InvalidRowHandle; } }
  491. protected override bool IsReadOnly { get { return Column.ReadOnly || View.DataProvider.IsColumnReadonly(Column.FieldName); } }
  492. protected override bool OverrideCellTemplate { get { return false; } }
  493. protected internal override bool CanShowEditor() {
  494. return Column.GetAllowEditing() && base.CanShowEditor();
  495. }
  496. protected internal override bool PostEditor() {
  497. if(Edit.IsValueChanged) {
  498. ValidateEditor();
  499. if(View.HasCellEditorError) {
  500. return false;
  501. }
  502. if(HasAccessToCellValue) {
  503. try {
  504. Grid.SetCellValue(RowData.RowHandle.Value, FieldNameCore, EditableValue);
  505. } catch(Exception e) {
  506. ShowError(new GridCellValidationError(e.Message, e, ErrorType.Default, RowData.RowHandle.Value, Column));
  507. return false;
  508. }
  509. RowData.UpdateData();
  510. }
  511. }
  512. return true;
  513. }
  514. protected internal override void ValidateEditor() {
  515. if(IsEditorVisible) {
  516. Edit.FlushPendingEditActions();
  517. ShowError(GetValidationError());
  518. }
  519. }
  520. protected override void OnHiddenEditor() {
  521. CellData.UpdateValue();
  522. base.OnHiddenEditor();
  523. }
  524. protected override void CancelRowEdit() {
  525. View.DataProvider.CancelCurrentRowEdit();
  526. View.OnCancelRowEdit();
  527. View.Grid.SetRowStateError(RowData.RowHandle.Value, null);
  528. }
  529. protected override void OnEditValueChanged() {
  530. base.OnEditValueChanged();
  531. View.OnFocusedRowCellModified();
  532. }
  533. void ShowError(GridCellValidationError validationError) {
  534. SetValidationError(null);
  535. if(validationError != null)
  536. SetValidationError(validationError);
  537. else
  538. RestoreValidationError();
  539. }
  540. GridCellValidationError GetValidationError() {
  541. ValidationResult validationResult;
  542. Exception exception = null;
  543. if(!Edit.DoValidate())
  544. return new GridCellValidationError("Invalid Value", null, ErrorType.Default, RowData.RowHandle.Value, Column);
  545. GridCellValidationEventArgs eventArgs = new GridCellValidationEventArgs(BaseEdit.ValidateEvent, this, EditableValue, CultureInfo.CurrentCulture, RowData.RowHandle.Value, View, Column);
  546. try {
  547. Column.OnValidation(eventArgs);
  548. validationResult = new ValidationResult(eventArgs.IsValid, eventArgs.ErrorContent);
  549. } catch(Exception e) {
  550. exception = e;
  551. validationResult = new ValidationResult(false, e.Message);
  552. }
  553. return validationResult.IsValid ? null : new GridCellValidationError(
  554. validationResult.ErrorContent,
  555. exception,
  556. eventArgs.ErrorType == ErrorType.None ? ErrorType.Default : eventArgs.ErrorType,
  557. RowData.RowHandle.Value,
  558. Column
  559. );
  560. }
  561. void SetValidationError(GridCellValidationError validationError) {
  562. View.ValidationError = validationError;
  563. BaseEditHelper.SetValidationError(RowData, validationError);
  564. BaseEditHelper.SetValidationError(CellData, validationError);
  565. Edit.SetValidationError(validationError);
  566. RowData.UpdateIndicatorState();
  567. }
  568. protected override void RestoreValidationError() {
  569. View.ValidationError = null;
  570. RowData.UpdateDataErrors();
  571. }
  572. protected override void UpdateValidationError() {
  573. Edit.SetValidationError(BaseEdit.GetValidationError(CellData));
  574. }
  575. void OnRowDataChanged(RowData oldValue) {
  576. UpdateData();
  577. if(!IsInTree)
  578. if(oldValue != null)
  579. ClearViewCurrentCellEditor(oldValue.View);
  580. }
  581. protected override bool IsProperEditorSettings() {
  582. return EditSettingsComparer.IsCompatibleEditSettings(editCore, Column.ActualEditSettings);
  583. }
  584. protected override void OnContentInvalidated() {
  585. base.OnContentInvalidated();
  586. if(editCore != null && EditorSourceType == BaseEditSourceType.CellTemplate)
  587. editCore.ClearValue(BaseEdit.EditValueProperty);
  588. }
  589. }
  590. public class FilterRowCellEditor : CellEditorBase {
  591. public FilterRowCellEditor() : base() {
  592. TableView.SetRowHandle(this, new RowHandle(RowHandle));
  593. }
  594. protected override int RowHandle { get { return GridControl.AutoFilterRowHandle; } }
  595. protected override bool IsReadOnly { get { return !Column.AllowAutoFilter; } }
  596. protected override bool OverrideCellTemplate { get { return true; } }
  597. protected override void UpdateEditContext() {
  598. }
  599. protected override void UpdateDisplayTemplate() {
  600. }
  601. protected override void UpdateEditTemplate() {
  602. }
  603. protected override BaseEdit CreateEditor(BaseEditSettings settings) {
  604. CheckEditSettings checkEditSettings = settings as CheckEditSettings;
  605. if(checkEditSettings != null) {
  606. CheckEdit checkEdit = (CheckEdit)checkEditSettings.CreateEditor(false, EmptyDefaultEditorViewInfo.Instance);
  607. checkEdit.IsThreeState = true;
  608. checkEdit.IsChecked = null;
  609. return checkEdit;
  610. }
  611. if(Column.ColumnFilterMode == ColumnFilterMode.DisplayText) {
  612. TextEdit textEdit = new TextEdit() { VerticalContentAlignment = settings.VerticalContentAlignment };
  613. settings.AssignViewInfoProperties(textEdit, Column);
  614. return textEdit;
  615. }
  616. ComboBoxEditSettings comboBoxEditSettings = settings as ComboBoxEditSettings;
  617. if(comboBoxEditSettings != null) {
  618. ComboBoxEdit comboBoxEdit = (ComboBoxEdit)comboBoxEditSettings.CreateEditor(false, EmptyDefaultEditorViewInfo.Instance);
  619. comboBoxEdit.ItemsSource = GetFilterComboBoxItems(comboBoxEdit.ItemsSource != null ? comboBoxEdit.ItemsSource : comboBoxEdit.Items);
  620. return comboBoxEdit;
  621. }
  622. return settings.CreateEditor(false, Column);
  623. }
  624. List<object> GetFilterComboBoxItems(IEnumerable items) {
  625. List<object> list = new List<object>();
  626. list.Add(new CustomComboBoxItem() { DisplayValue = string.Empty, EditValue = string.Empty });
  627. foreach(object item in items)
  628. list.Add(item);
  629. return list;
  630. }
  631. protected override void UpdateEditValue(BaseEdit editor) {
  632. editor.EditValue = Column.AutoFilterValue;
  633. }
  634. protected internal override bool PostEditor() {
  635. if(Edit.IsValueChanged) {
  636. Edit.FlushPendingEditActions();
  637. Column.AutoFilterValue = Edit.EditValue;
  638. }
  639. return true;
  640. }
  641. protected override void OnEditValueChanged() {
  642. if(Column.ImmediateUpdateAutoFilter)
  643. Column.AutoFilterValue = Edit.EditValue;
  644. }
  645. protected override void OnHiddenEditor() {
  646. Edit.EditValue = Column.AutoFilterValue;
  647. base.OnHiddenEditor();
  648. }
  649. protected override void OnColumnContentChanged(object sender, ColumnContentChangedEventArgs e) {
  650. if(e.Property == GridColumn.AutoFilterValueProperty) {
  651. Edit.EditValue = Column.AutoFilterValue;
  652. return;
  653. }
  654. if (e.Property == GridColumn.ColumnFilterModeProperty) {
  655. UpdateContent();
  656. return;
  657. }
  658. base.OnColumnContentChanged(sender, e);
  659. }
  660. protected override void SetDisplayTextProvider(BaseEdit newEdit) {
  661. }
  662. }
  663. public class NewItemRowCellEditor : CellEditor {
  664. protected override void OnEditValueChanged() {
  665. base.OnEditValueChanged();
  666. if(View.IsNewItemRowFocused && !View.DataProvider.IsNewItemRowEditing)
  667. (View as TableView).BeginNewItemRow();
  668. }
  669. protected override void OnPreviewKeyDown(KeyEventArgs e) {
  670. base.OnPreviewKeyDown(e);
  671. if(e.Key == Key.Enter && !IsEditorVisible)
  672. (View as TableView).MoveNextNewItemRowCell();
  673. }
  674. }
  675. public class CustomComboBoxItem : ICustomComboBoxItem {
  676. object displayValue;
  677. object editValue;
  678. public object DisplayValue {
  679. get { return displayValue; }
  680. set { displayValue = value; }
  681. }
  682. public object EditValue {
  683. get { return editValue; }
  684. set { editValue = value; }
  685. }
  686. }
  687. public interface IBaseEdit {
  688. void LockEditorFocus();
  689. void UnlockEditorFocus();
  690. bool NeedsKey(KeyEventArgs e);
  691. bool IsActivatingKey(KeyEventArgs e);
  692. void ProcessActivatingKey(KeyEventArgs e);
  693. bool IsReadOnly { get; set; }
  694. bool IsEditButtonsVisible { get; set; }
  695. bool IsEditorActive { get; }
  696. bool IsValueChanged { get; set; }
  697. EditMode EditMode { get; set; }
  698. object EditValue { get; set; }
  699. void SetKeyboardFocus();
  700. void SetValidationError(BaseValidationError validationError);
  701. bool IsChildElement(IInputElement element);
  702. void SetDisplayTemplate(ControlTemplate template);
  703. void SetEditTemplate(ControlTemplate template);
  704. void SelectAll();
  705. event EditValueChangedEventHandler EditValueChanged;
  706. void FlushPendingEditActions();
  707. bool DoValidate();
  708. }
  709. public class BaseEditWrapper : IBaseEdit {
  710. BaseEdit edit;
  711. public BaseEditWrapper(BaseEdit edit) {
  712. this.edit = edit;
  713. }
  714. void IBaseEdit.LockEditorFocus() {
  715. BaseEditHelper.LockEditorFocus(edit);
  716. }
  717. void IBaseEdit.UnlockEditorFocus() {
  718. BaseEditHelper.UnlockEditorFocus(edit);
  719. }
  720. bool IBaseEdit.NeedsKey(KeyEventArgs e) {
  721. return BaseEditHelper.GetNeedsKey(edit, e);
  722. }
  723. bool IBaseEdit.IsActivatingKey(KeyEventArgs e) {
  724. return BaseEditHelper.GetIsActivatingKey(edit, e);
  725. }
  726. void IBaseEdit.ProcessActivatingKey(KeyEventArgs e) {
  727. BaseEditHelper.ProcessActivatingKey(edit, e);
  728. }
  729. bool IBaseEdit.IsReadOnly { get { return edit.IsReadOnly; } set { edit.IsReadOnly = value; } }
  730. bool IBaseEdit.IsEditButtonsVisible { get { return edit.IsEditButtonsVisible; } set { edit.IsEditButtonsVisible = value; } }
  731. bool IBaseEdit.IsEditorActive { get { return edit.IsEditorActive; } }
  732. bool IBaseEdit.IsValueChanged { get { return BaseEditHelper.GetIsValueChanged(edit); } set { BaseEditHelper.SetIsValueChanged(edit, value); } }
  733. EditMode IBaseEdit.EditMode { get { return edit.EditMode; } set { edit.EditMode = value; } }
  734. object IBaseEdit.EditValue { get { return edit.EditValue; } set { edit.EditValue = value; } }
  735. void IBaseEdit.SetKeyboardFocus() {
  736. Keyboard.Focus(edit);
  737. }
  738. void IBaseEdit.SetValidationError(BaseValidationError validationError) {
  739. BaseEditHelper.SetValidationError(edit, validationError);
  740. }
  741. bool IBaseEdit.IsChildElement(IInputElement element) {
  742. return BaseEditHelper.GetIsChildElement(edit, element);
  743. }
  744. void IBaseEdit.SetDisplayTemplate(ControlTemplate template) {
  745. if(template != null)
  746. edit.DisplayTemplate = template;
  747. else
  748. edit.ClearValue(BaseEdit.DisplayTemplateProperty);
  749. }
  750. void IBaseEdit.SetEditTemplate(ControlTemplate template) {
  751. if(template != null)
  752. edit.EditTemplate = template;
  753. else
  754. edit.ClearValue(BaseEdit.EditTemplateProperty);
  755. }
  756. void IBaseEdit.SelectAll() {
  757. edit.SelectAll();
  758. }
  759. event EditValueChangedEventHandler IBaseEdit.EditValueChanged { 
  760. add { edit.EditValueChanged += value; } 
  761. remove { edit.EditValueChanged -= value; } 
  762. }
  763. void IBaseEdit.FlushPendingEditActions() {
  764. BaseEditHelper.FlushPendingEditActions(edit);
  765. }
  766. bool IBaseEdit.DoValidate() {
  767. return edit.DoValidate();
  768. }
  769. }
  770. public class FakeBaseEdit : IBaseEdit {
  771. public static readonly IBaseEdit Instance = new FakeBaseEdit();
  772. private FakeBaseEdit () {
  773. }
  774. void IBaseEdit.LockEditorFocus() { }
  775. void IBaseEdit.UnlockEditorFocus() { }
  776. bool IBaseEdit.NeedsKey(KeyEventArgs e) {
  777. return true;
  778. }
  779. bool IBaseEdit.IsActivatingKey(KeyEventArgs e) {
  780. return false;
  781. }
  782. void IBaseEdit.ProcessActivatingKey(KeyEventArgs e) { }
  783. bool IBaseEdit.IsReadOnly { get { return true; } set { } }
  784. bool IBaseEdit.IsEditButtonsVisible { get { return false; } set { } }
  785. bool IBaseEdit.IsEditorActive { get { return false; } }
  786. bool IBaseEdit.IsValueChanged { get { return false; } set { } }
  787. EditMode IBaseEdit.EditMode { get { return EditMode.InplaceInactive; } set { } }
  788. object IBaseEdit.EditValue { get { return null; } set { } }
  789. void IBaseEdit.SetKeyboardFocus() { }
  790. void IBaseEdit.SetValidationError(BaseValidationError validationError) { }
  791. bool IBaseEdit.IsChildElement(IInputElement element) { return false; }
  792. void IBaseEdit.SetDisplayTemplate(ControlTemplate template) { }
  793. void IBaseEdit.SetEditTemplate(ControlTemplate template) { }
  794. void IBaseEdit.SelectAll() { }
  795. event EditValueChangedEventHandler IBaseEdit.EditValueChanged { add { } remove { } }
  796. void IBaseEdit.FlushPendingEditActions() { }
  797. bool IBaseEdit.DoValidate() { return true; }
  798. }
  799. }