diff --git a/CS/GridControlCRUD.Common/View/GridControlDeleteRefreshBehavior.cs b/CS/GridControlCRUD.Common/View/GridControlDeleteRefreshBehavior.cs index b8bfa27..07ee3bc 100644 --- a/CS/GridControlCRUD.Common/View/GridControlDeleteRefreshBehavior.cs +++ b/CS/GridControlCRUD.Common/View/GridControlDeleteRefreshBehavior.cs @@ -1,5 +1,4 @@ -using DevExpress.CRUD.ViewModel; -using DevExpress.Mvvm; +using DevExpress.Mvvm; using DevExpress.Mvvm.UI.Interactivity; using DevExpress.Xpf.Core; using DevExpress.Xpf.Data; @@ -9,16 +8,10 @@ using System.Threading.Tasks; using System.Windows; using System.Windows.Input; +using DevExpress.CRUD.ViewModel; namespace DevExpress.CRUD.View { public class GridControlDeleteRefreshBehavior : Behavior { - public ICommand OnDeleteCommand { - get { return (ICommand)GetValue(OnDeleteCommandProperty); } - set { SetValue(OnDeleteCommandProperty, value); } - } - public static readonly DependencyProperty OnDeleteCommandProperty = - DependencyProperty.Register("OnDeleteCommand", typeof(ICommand), typeof(GridControlDeleteRefreshBehavior), new PropertyMetadata(null)); - public ICommand OnRefreshCommand { get { return (ICommand)GetValue(OnRefreshCommandProperty); } set { SetValue(OnRefreshCommandProperty, value); } @@ -34,14 +27,12 @@ public string NoRecordsErrorMessage { DependencyProperty.Register("NoRecordsErrorMessage", typeof(string), typeof(GridControlDeleteRefreshBehavior), new PropertyMetadata(null, (d, e) => { ((GridControlDeleteRefreshBehavior)d).UpdateErrorText(); })); - public ICommand DeleteCommand { get; } public ICommand RefreshCommand { get; } TableView View => AssociatedObject; VirtualSourceBase VirtualSource => View?.DataControl?.ItemsSource as VirtualSourceBase; public GridControlDeleteRefreshBehavior() { - DeleteCommand = new DelegateCommand(DoDelete, CanDelete); RefreshCommand = new AsyncCommand(DoRefresh, CanRefresh); } @@ -72,10 +63,6 @@ void UpdateErrorText() { } async void OnPreviewKeyDown(object sender, KeyEventArgs e) { - if(e.Key == Key.Delete && CanDelete()) { - e.Handled = true; - DoDelete(); - } if(e.Key == Key.F5 && CanRefresh()) { e.Handled = true; await DoRefresh(); @@ -100,33 +87,12 @@ bool CanRefresh() { var canRefreshVirtualSource = VirtualSource == null || ((VirtualSource as InfiniteAsyncSource)?.IsResetting != true && !VirtualSource.AreRowsFetching); return canRefreshVirtualSource - && OnRefreshCommand != null - && !IsEditingRowState() + && OnRefreshCommand != null + && !IsEditingRowState() && !isRefreshInProgress && (View?.Grid.ItemsSource != null || NoRecordsErrorMessage != null); } - void DoDelete() { - var row = View.Grid.SelectedItem; - if(row == null) - return; - if(DXMessageBox.Show(View, "Are you sure you want to delete this row?", "Delete Row", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel) - return; - try { - OnDeleteCommand.Execute(new RowDeleteArgs(row)); - if(VirtualSource != null) - VirtualSource?.RefreshRows(); - else - View.Commands.DeleteFocusedRow.Execute(null); - } catch(Exception ex) { - DXMessageBox.Show(ex.Message); - } - } - - bool CanDelete() { - return OnDeleteCommand != null && !IsEditingRowState() && !isRefreshInProgress && View?.Grid.CurrentItem != null; - } - bool IsEditingRowState() { return View?.AreUpdateRowButtonsShown == true; } diff --git a/CS/GridControlCRUD.Common/ViewModel/AsyncCollectionViewModel.cs b/CS/GridControlCRUD.Common/ViewModel/AsyncCollectionViewModel.cs index a4e69df..51d658b 100644 --- a/CS/GridControlCRUD.Common/ViewModel/AsyncCollectionViewModel.cs +++ b/CS/GridControlCRUD.Common/ViewModel/AsyncCollectionViewModel.cs @@ -2,6 +2,7 @@ using DevExpress.Mvvm; using DevExpress.Mvvm.DataAnnotations; using DevExpress.Mvvm.Xpf; +using System; using System.Collections.Generic; using System.Threading.Tasks; @@ -9,6 +10,8 @@ namespace DevExpress.CRUD.ViewModel { public abstract class AsyncCollectionViewModel : ViewModelBase where T : class { readonly IDataProvider dataProvider; + IMessageBoxService MessageBoxService => GetService(); + protected AsyncCollectionViewModel(IDataProvider dataProvider) { this.dataProvider = dataProvider; StartRefresh(); @@ -31,6 +34,26 @@ async void StartRefresh() { await OnRefreshAsync(); } + [Command] + public void OnDeleteRow(DeleteRowValidationArgs args) => OnDelete(args); + + void OnDelete(DeleteRowValidationArgs args) { + if(MessageBoxService.ShowMessage("Are you sure you want to delete this row?", "Delete Row", MessageButton.OKCancel) == MessageResult.Cancel) { + args.Result = "Not accepted"; + return; + } + try { + if(IsLoading) { + args.Result = "Data is refrehsing"; + return; + } + dataProvider.Delete((T)args.Items[0]); + } catch(Exception ex) { + MessageBoxService.ShowMessage(ex.Message); + args.Result = ex.Message; + } + } + [Command] public void OnRefresh(RefreshArgs args) { args.Result = OnRefreshAsync(); @@ -67,8 +90,5 @@ async Task OnUpdateRowAsync(T entity, bool isNew) { await dataProvider.UpdateAsync(entity); return null; } - - [Command] - public void OnDelete(RowDeleteArgs args) => dataProvider.Delete((T)args.Row); } } diff --git a/CS/GridControlCRUD.Common/ViewModel/CollectionViewModel.cs b/CS/GridControlCRUD.Common/ViewModel/CollectionViewModel.cs index f21a0a9..0d7aa12 100644 --- a/CS/GridControlCRUD.Common/ViewModel/CollectionViewModel.cs +++ b/CS/GridControlCRUD.Common/ViewModel/CollectionViewModel.cs @@ -2,12 +2,17 @@ using DevExpress.Mvvm; using DevExpress.Mvvm.DataAnnotations; using DevExpress.Mvvm.Xpf; +using DevExpress.Xpf.Core; +using System; using System.Collections.Generic; +using System.Windows; namespace DevExpress.CRUD.ViewModel { public abstract class CollectionViewModel : ViewModelBase where T : class { readonly IDataProvider dataProvider; + IMessageBoxService MessageBoxService => GetService(); + protected CollectionViewModel(IDataProvider dataProvider) { this.dataProvider = dataProvider; OnRefresh(); @@ -22,6 +27,22 @@ public string EntitiesErrorMessage { private set => SetValue(value); } + [Command] + public void OnDeleteRow(DeleteRowValidationArgs args) => OnDelete(args); + + void OnDelete(DeleteRowValidationArgs args) { + if(MessageBoxService.ShowMessage("Are you sure you want to delete this row?", "Delete Row", MessageButton.OKCancel) == MessageResult.Cancel) { + args.Result = "Not accepted"; + return; + } + try { + dataProvider.Delete((T)args.Items[0]); + } catch (Exception ex) { + MessageBoxService.ShowMessage(ex.Message); + args.Result = ex.Message; + } + } + [Command] public void OnRefresh(RefreshArgs _) { OnRefresh(); @@ -48,8 +69,5 @@ public void OnUpdateRow(RowValidationArgs args) { else dataProvider.Update(entity); } - - [Command] - public void OnDelete(RowDeleteArgs args) => dataProvider.Delete((T)args.Row); } } diff --git a/CS/GridControlCRUD.Common/ViewModel/VirtualCollectionViewModel.cs b/CS/GridControlCRUD.Common/ViewModel/VirtualCollectionViewModel.cs index ede931b..8255784 100644 --- a/CS/GridControlCRUD.Common/ViewModel/VirtualCollectionViewModel.cs +++ b/CS/GridControlCRUD.Common/ViewModel/VirtualCollectionViewModel.cs @@ -15,6 +15,8 @@ namespace DevExpress.CRUD.ViewModel { public abstract class VirtualCollectionViewModel : ViewModelBase where T : class, new() { readonly IDataProvider dataProvider; + IMessageBoxService MessageBoxService => GetService(); + protected VirtualCollectionViewModel(IDataProvider dataProvider) { this.dataProvider = dataProvider; StartRefresh(); @@ -90,10 +92,21 @@ UICommand[] CreateCommands(Action saveAction) { protected abstract EntityViewModel CreateEntityViewModel(T entity); [Command] - public void OnDelete(RowDeleteArgs args) { - this.dataProvider.Delete((T)args.Row); - } + public void OnDeleteRow(DeleteRowValidationArgs args) => OnDelete(args); + void OnDelete(DeleteRowValidationArgs args) { + if(MessageBoxService.ShowMessage("Are you sure you want to delete this row?", "Delete Row", MessageButton.OKCancel) == MessageResult.Cancel) { + args.Result = "Not accepted"; + return; + } + try { + dataProvider.Delete((T)args.Items[0]); + } catch(Exception ex) { + MessageBoxService.ShowMessage(ex.Message); + args.Result = ex.Message; + } + } + [Command] public void OnRefresh(RefreshArgs args) { args.Result = OnRefreshCoreAsync(); diff --git a/CS/GridControlCRUDMVVM/GridControlCRUDMVVM.csproj b/CS/GridControlCRUDMVVM/GridControlCRUDMVVM.csproj index 0b0966d..81526d3 100644 --- a/CS/GridControlCRUDMVVM/GridControlCRUDMVVM.csproj +++ b/CS/GridControlCRUDMVVM/GridControlCRUDMVVM.csproj @@ -39,18 +39,18 @@ 4 - - - + + + True - - - - - - - + + + + + + + ..\packages\EntityFramework.6.4.0\lib\net45\EntityFramework.dll diff --git a/CS/GridControlCRUDMVVM/MainWindow.xaml b/CS/GridControlCRUDMVVM/MainWindow.xaml index eb6ffde..3cc9f6c 100644 --- a/CS/GridControlCRUDMVVM/MainWindow.xaml +++ b/CS/GridControlCRUDMVVM/MainWindow.xaml @@ -15,6 +15,9 @@ + + + @@ -22,9 +25,12 @@ - + + + + @@ -36,10 +42,11 @@ AutoWidth="True" ShowUpdateRowButtons="OnCellEditorOpen" NewItemRowPosition="Top" - ValidateRowCommand="{Binding OnUpdateRowCommand}"> + ValidateRowCommand="{Binding OnUpdateRowCommand}" + ValidateDeleteRowCommand="{Binding OnDeleteRowCommand}"> + OnRefreshCommand="{Binding OnRefreshCommand}"/> diff --git a/CS/GridControlCRUDMVVMAsync/GridControlCRUDMVVMAsync.csproj b/CS/GridControlCRUDMVVMAsync/GridControlCRUDMVVMAsync.csproj index e240411..91a464f 100644 --- a/CS/GridControlCRUDMVVMAsync/GridControlCRUDMVVMAsync.csproj +++ b/CS/GridControlCRUDMVVMAsync/GridControlCRUDMVVMAsync.csproj @@ -39,18 +39,18 @@ 4 - - - + + + True - - - - - - - + + + + + + + ..\packages\EntityFramework.6.4.0\lib\net45\EntityFramework.dll diff --git a/CS/GridControlCRUDMVVMAsync/MainWindow.xaml b/CS/GridControlCRUDMVVMAsync/MainWindow.xaml index fa9b8f7..b940635 100644 --- a/CS/GridControlCRUDMVVMAsync/MainWindow.xaml +++ b/CS/GridControlCRUDMVVMAsync/MainWindow.xaml @@ -22,9 +22,12 @@ - + + + + @@ -36,10 +39,11 @@ AutoWidth="True" ShowUpdateRowButtons="OnCellEditorOpen" NewItemRowPosition="Top" - ValidateRowCommand="{Binding OnUpdateRowCommand}"> + ValidateRowCommand="{Binding OnUpdateRowCommand}" + ValidateDeleteRowCommand="{Binding OnDeleteRowCommand}"> + OnRefreshCommand="{Binding OnRefreshCommand}"/> diff --git a/CS/GridControlCRUDMVVMInfiniteAsyncSource/GridControlCRUDMVVMInfiniteAsyncSource.csproj b/CS/GridControlCRUDMVVMInfiniteAsyncSource/GridControlCRUDMVVMInfiniteAsyncSource.csproj index b126b96..c295a93 100644 --- a/CS/GridControlCRUDMVVMInfiniteAsyncSource/GridControlCRUDMVVMInfiniteAsyncSource.csproj +++ b/CS/GridControlCRUDMVVMInfiniteAsyncSource/GridControlCRUDMVVMInfiniteAsyncSource.csproj @@ -39,19 +39,19 @@ 4 - - - + + + True - - - - - - - - + + + + + + + + ..\packages\EntityFramework.6.4.0\lib\net45\EntityFramework.dll diff --git a/CS/GridControlCRUDMVVMInfiniteAsyncSource/MainWindow.xaml b/CS/GridControlCRUDMVVMInfiniteAsyncSource/MainWindow.xaml index a991e2b..44ed87f 100644 --- a/CS/GridControlCRUDMVVMInfiniteAsyncSource/MainWindow.xaml +++ b/CS/GridControlCRUDMVVMInfiniteAsyncSource/MainWindow.xaml @@ -48,9 +48,12 @@ - + + + + - + + OnRefreshCommand="{Binding OnRefreshCommand}"/> diff --git a/CS/GridControlCRUDSimple/GridControlCRUDSimple.csproj b/CS/GridControlCRUDSimple/GridControlCRUDSimple.csproj index 436777a..df3c2a6 100644 --- a/CS/GridControlCRUDSimple/GridControlCRUDSimple.csproj +++ b/CS/GridControlCRUDSimple/GridControlCRUDSimple.csproj @@ -39,15 +39,15 @@ 4 - - - - - - - - - + + + + + + + + + ..\packages\EntityFramework.6.4.0\lib\net45\EntityFramework.dll diff --git a/CS/GridControlCRUDSimple/MainWindow.xaml b/CS/GridControlCRUDSimple/MainWindow.xaml index f7f42b4..a2ae33c 100644 --- a/CS/GridControlCRUDSimple/MainWindow.xaml +++ b/CS/GridControlCRUDSimple/MainWindow.xaml @@ -1,4 +1,4 @@ - - + + + + @@ -19,7 +22,7 @@ + ValidateRow="tableView_ValidateRow" ValidateDeleteRow="ValidateRowDeletion" NewItemRowPosition="Top"/> diff --git a/CS/GridControlCRUDSimple/MainWindow.xaml.cs b/CS/GridControlCRUDSimple/MainWindow.xaml.cs index c0203e3..efaab14 100644 --- a/CS/GridControlCRUDSimple/MainWindow.xaml.cs +++ b/CS/GridControlCRUDSimple/MainWindow.xaml.cs @@ -6,6 +6,7 @@ using System.Windows; using System.Windows.Input; using DevExpress.CRUD.Northwind.DataModel; +using DevExpress.Mvvm; namespace GridControlCRUDSimple { public partial class MainWindow : ThemedWindow { @@ -52,26 +53,24 @@ void tableView_ValidateRow(object sender, GridRowValidationEventArgs e) { } } - void grid_KeyDown(object sender, KeyEventArgs e) { - if(e.Key == Key.Delete) { - var productInfo = (ProductInfo)grid.SelectedItem; - if(productInfo == null) - return; - if(DXMessageBox.Show(this, "Are you sure you want to delete this row?", "Delete Row", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel) - return; - try { - using(var context = new NorthwindContext()) { - var result = context.Products.Find(productInfo.Id); - if(result == null) { - throw new NotImplementedException("The modified row no longer exists in the database. Handle this case according to your requirements."); - } - context.Products.Remove(result); - context.SaveChanges(); - view.Commands.DeleteFocusedRow.Execute(null); + private void ValidateRowDeletion(object sender, GridDeleteRowValidationEventArgs e) { + if(DXMessageBox.Show(this, "Are you sure you want to delete this row?", "Delete Row", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel) { + e.Result = "Not accepted"; + return; + } + try { + using(var context = new NorthwindContext()) { + var result = context.Products.Find(((ProductInfo)e.Rows[0]).Id); + if(result == null) { + e.Result = "The modified row no longer exists in the database. Handle this case according to your requirements."; + return; } - } catch(Exception ex) { - DXMessageBox.Show(ex.Message); + context.Products.Remove(result); + context.SaveChanges(); } + } catch(Exception ex) { + DXMessageBox.Show(ex.Message); + e.Result = ex.Message; } } } diff --git a/Readme.md b/Readme.md index 78ced2a..49b6fe9 100644 --- a/Readme.md +++ b/Readme.md @@ -1,5 +1,5 @@ -![](https://img.shields.io/endpoint?url=https://codecentral.devexpress.com/api/v1/VersionRange/265491908/21.1.2%2B) +![](https://img.shields.io/endpoint?url=https://codecentral.devexpress.com/api/v1/VersionRange/265491908/21.2.2%2B) [![](https://img.shields.io/badge/Open_in_DevExpress_Support_Center-FF7200?style=flat-square&logo=DevExpress&logoColor=white)](https://supportcenter.devexpress.com/ticket/details/T899930) [![](https://img.shields.io/badge/📖_How_to_use_DevExpress_Examples-e9f6fc?style=flat-square)](https://docs.devexpress.com/GeneralInformation/403183)