Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 4 additions & 38 deletions CS/GridControlCRUD.Common/View/GridControlDeleteRefreshBehavior.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<TableView> {
public ICommand<RowDeleteArgs> OnDeleteCommand {
get { return (ICommand<RowDeleteArgs>)GetValue(OnDeleteCommandProperty); }
set { SetValue(OnDeleteCommandProperty, value); }
}
public static readonly DependencyProperty OnDeleteCommandProperty =
DependencyProperty.Register("OnDeleteCommand", typeof(ICommand<RowDeleteArgs>), typeof(GridControlDeleteRefreshBehavior), new PropertyMetadata(null));

public ICommand<RefreshArgs> OnRefreshCommand {
get { return (ICommand<RefreshArgs>)GetValue(OnRefreshCommandProperty); }
set { SetValue(OnRefreshCommandProperty, value); }
Expand All @@ -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);
}

Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
Expand Down
26 changes: 23 additions & 3 deletions CS/GridControlCRUD.Common/ViewModel/AsyncCollectionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm.Xpf;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace DevExpress.CRUD.ViewModel {
public abstract class AsyncCollectionViewModel<T> : ViewModelBase where T : class {
readonly IDataProvider<T> dataProvider;

IMessageBoxService MessageBoxService => GetService<IMessageBoxService>();

protected AsyncCollectionViewModel(IDataProvider<T> dataProvider) {
this.dataProvider = dataProvider;
StartRefresh();
Expand All @@ -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();
Expand Down Expand Up @@ -67,8 +90,5 @@ async Task<ValidationErrorInfo> OnUpdateRowAsync(T entity, bool isNew) {
await dataProvider.UpdateAsync(entity);
return null;
}

[Command]
public void OnDelete(RowDeleteArgs args) => dataProvider.Delete((T)args.Row);
}
}
24 changes: 21 additions & 3 deletions CS/GridControlCRUD.Common/ViewModel/CollectionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> : ViewModelBase where T : class {
readonly IDataProvider<T> dataProvider;

IMessageBoxService MessageBoxService => GetService<IMessageBoxService>();

protected CollectionViewModel(IDataProvider<T> dataProvider) {
this.dataProvider = dataProvider;
OnRefresh();
Expand All @@ -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();
Expand All @@ -48,8 +69,5 @@ public void OnUpdateRow(RowValidationArgs args) {
else
dataProvider.Update(entity);
}

[Command]
public void OnDelete(RowDeleteArgs args) => dataProvider.Delete((T)args.Row);
}
}
19 changes: 16 additions & 3 deletions CS/GridControlCRUD.Common/ViewModel/VirtualCollectionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace DevExpress.CRUD.ViewModel {
public abstract class VirtualCollectionViewModel<T> : ViewModelBase where T : class, new() {
readonly IDataProvider<T> dataProvider;

IMessageBoxService MessageBoxService => GetService<IMessageBoxService>();

protected VirtualCollectionViewModel(IDataProvider<T> dataProvider) {
this.dataProvider = dataProvider;
StartRefresh();
Expand Down Expand Up @@ -90,10 +92,21 @@ UICommand[] CreateCommands(Action saveAction) {
protected abstract EntityViewModel<T> 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();
Expand Down
20 changes: 10 additions & 10 deletions CS/GridControlCRUDMVVM/GridControlCRUDMVVM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="DevExpress.Data.v21.1" />
<Reference Include="DevExpress.Data.Desktop.v21.1" />
<Reference Include="DevExpress.Images.v21.1">
<Reference Include="DevExpress.Data.v21.2" />
<Reference Include="DevExpress.Data.Desktop.v21.2" />
<Reference Include="DevExpress.Images.v21.2">
<Private>True</Private>
</Reference>
<Reference Include="DevExpress.Mvvm.v21.1" />
<Reference Include="DevExpress.Printing.v21.1.Core" />
<Reference Include="DevExpress.Xpf.Core.v21.1" />
<Reference Include="DevExpress.Xpf.Printing.v21.1" />
<Reference Include="DevExpress.Xpf.Grid.v21.1" />
<Reference Include="DevExpress.Xpf.Grid.v21.1.Core" />
<Reference Include="DevExpress.Xpf.Grid.v21.1.Extensions" />
<Reference Include="DevExpress.Mvvm.v21.2" />
<Reference Include="DevExpress.Printing.v21.2.Core" />
<Reference Include="DevExpress.Xpf.Core.v21.2" />
<Reference Include="DevExpress.Xpf.Printing.v21.2" />
<Reference Include="DevExpress.Xpf.Grid.v21.2" />
<Reference Include="DevExpress.Xpf.Grid.v21.2.Core" />
<Reference Include="DevExpress.Xpf.Grid.v21.2.Extensions" />
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.4.0\lib\net45\EntityFramework.dll</HintPath>
</Reference>
Expand Down
13 changes: 10 additions & 3 deletions CS/GridControlCRUDMVVM/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,22 @@
<Window.DataContext>
<northwind_viewModel:ProductCollectionViewModel/>
</Window.DataContext>
<dxmvvm:Interaction.Behaviors>
<dx:DXMessageBoxService/>
</dxmvvm:Interaction.Behaviors>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<dxb:ToolBarControl>
<dxb:BarButtonItem Content="Refresh (F5)" Command="{Binding RefreshCommand, ElementName=DeleteRefreshBehavior}" BarItemDisplayMode="ContentAndGlyph" Glyph="{dx:DXImage SvgImages/Icon Builder/Actions_Refresh.svg}"/>
<dxb:BarButtonItem Content="Delete (Del)" Command="{Binding DeleteCommand, ElementName=DeleteRefreshBehavior}" BarItemDisplayMode="ContentAndGlyph" Glyph="{dx:DXImage SvgImages/Icon Builder/Actions_Delete.svg}"/>
<dxb:BarButtonItem Content="Delete (Del)" Command="dxg:GridCommands.DeleteFocusedRow" CommandTarget="{Binding ElementName=view}" BarItemDisplayMode="ContentAndGlyph" Glyph="{dx:DXImage SvgImages/Icon Builder/Actions_Delete.svg}"/>
</dxb:ToolBarControl>
<dxg:GridControl Grid.Row="1" x:Name="grid" ItemsSource="{Binding Entities}">
<dxg:GridControl.InputBindings>
<KeyBinding Command="{Binding Commands.DeleteFocusedRow, ElementName=view}" Key="Delete"/>
</dxg:GridControl.InputBindings>
<dxg:GridColumn FieldName="Name" Width="2*"/>
<dxg:GridColumn FieldName="CategoryId" Header="Category" Width="*">
<dxg:GridColumn.EditSettings>
Expand All @@ -36,10 +42,11 @@
AutoWidth="True"
ShowUpdateRowButtons="OnCellEditorOpen"
NewItemRowPosition="Top"
ValidateRowCommand="{Binding OnUpdateRowCommand}">
ValidateRowCommand="{Binding OnUpdateRowCommand}"
ValidateDeleteRowCommand="{Binding OnDeleteRowCommand}">
<dxmvvm:Interaction.Behaviors>
<view:GridControlDeleteRefreshBehavior x:Name="DeleteRefreshBehavior" NoRecordsErrorMessage="{Binding EntitiesErrorMessage}"
OnDeleteCommand="{Binding OnDeleteCommand}" OnRefreshCommand="{Binding OnRefreshCommand}"/>
OnRefreshCommand="{Binding OnRefreshCommand}"/>
</dxmvvm:Interaction.Behaviors>
</dxg:TableView>
</dxg:GridControl.View>
Expand Down
20 changes: 10 additions & 10 deletions CS/GridControlCRUDMVVMAsync/GridControlCRUDMVVMAsync.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="DevExpress.Data.v21.1" />
<Reference Include="DevExpress.Data.Desktop.v21.1" />
<Reference Include="DevExpress.Images.v21.1">
<Reference Include="DevExpress.Data.v21.2" />
<Reference Include="DevExpress.Data.Desktop.v21.2" />
<Reference Include="DevExpress.Images.v21.2">
<Private>True</Private>
</Reference>
<Reference Include="DevExpress.Mvvm.v21.1" />
<Reference Include="DevExpress.Printing.v21.1.Core" />
<Reference Include="DevExpress.Xpf.Core.v21.1" />
<Reference Include="DevExpress.Xpf.Printing.v21.1" />
<Reference Include="DevExpress.Xpf.Grid.v21.1" />
<Reference Include="DevExpress.Xpf.Grid.v21.1.Core" />
<Reference Include="DevExpress.Xpf.Grid.v21.1.Extensions" />
<Reference Include="DevExpress.Mvvm.v21.2" />
<Reference Include="DevExpress.Printing.v21.2.Core" />
<Reference Include="DevExpress.Xpf.Core.v21.2" />
<Reference Include="DevExpress.Xpf.Printing.v21.2" />
<Reference Include="DevExpress.Xpf.Grid.v21.2" />
<Reference Include="DevExpress.Xpf.Grid.v21.2.Core" />
<Reference Include="DevExpress.Xpf.Grid.v21.2.Extensions" />
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.4.0\lib\net45\EntityFramework.dll</HintPath>
</Reference>
Expand Down
10 changes: 7 additions & 3 deletions CS/GridControlCRUDMVVMAsync/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
</Grid.RowDefinitions>
<dxb:ToolBarControl>
<dxb:BarButtonItem Content="Refresh (F5)" Command="{Binding RefreshCommand, ElementName=DeleteRefreshBehavior}" BarItemDisplayMode="ContentAndGlyph" Glyph="{dx:DXImage SvgImages/Icon Builder/Actions_Refresh.svg}"/>
<dxb:BarButtonItem Content="Delete (Del)" Command="{Binding DeleteCommand, ElementName=DeleteRefreshBehavior}" BarItemDisplayMode="ContentAndGlyph" Glyph="{dx:DXImage SvgImages/Icon Builder/Actions_Delete.svg}"/>
<dxb:BarButtonItem Content="Delete (Del)" Command="dxg:GridCommands.DeleteFocusedRow" CommandTarget="{Binding ElementName=view}" BarItemDisplayMode="ContentAndGlyph" Glyph="{dx:DXImage SvgImages/Icon Builder/Actions_Delete.svg}"/>
</dxb:ToolBarControl>
<dxg:GridControl Grid.Row="1" x:Name="grid" ItemsSource="{Binding Entities}" ShowLoadingPanel="{Binding IsLoading}">
<dxg:GridControl.InputBindings>
<KeyBinding Command="{Binding Commands.DeleteFocusedRow, ElementName=view}" Key="Delete"/>
</dxg:GridControl.InputBindings>
<dxg:GridColumn FieldName="Name" Width="2*"/>
<dxg:GridColumn FieldName="CategoryId" Header="Category" Width="*">
<dxg:GridColumn.EditSettings>
Expand All @@ -36,10 +39,11 @@
AutoWidth="True"
ShowUpdateRowButtons="OnCellEditorOpen"
NewItemRowPosition="Top"
ValidateRowCommand="{Binding OnUpdateRowCommand}">
ValidateRowCommand="{Binding OnUpdateRowCommand}"
ValidateDeleteRowCommand="{Binding OnDeleteRowCommand}">
<dxmvvm:Interaction.Behaviors>
<view:GridControlDeleteRefreshBehavior x:Name="DeleteRefreshBehavior" NoRecordsErrorMessage="{Binding EntitiesErrorMessage}"
OnDeleteCommand="{Binding OnDeleteCommand}" OnRefreshCommand="{Binding OnRefreshCommand}"/>
OnRefreshCommand="{Binding OnRefreshCommand}"/>
</dxmvvm:Interaction.Behaviors>
</dxg:TableView>
</dxg:GridControl.View>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="DevExpress.Data.v21.1" />
<Reference Include="DevExpress.Data.Desktop.v21.1" />
<Reference Include="DevExpress.Images.v21.1">
<Reference Include="DevExpress.Data.v21.2" />
<Reference Include="DevExpress.Data.Desktop.v21.2" />
<Reference Include="DevExpress.Images.v21.2">
<Private>True</Private>
</Reference>
<Reference Include="DevExpress.Mvvm.v21.1" />
<Reference Include="DevExpress.Printing.v21.1.Core" />
<Reference Include="DevExpress.Xpf.Core.v21.1" />
<Reference Include="DevExpress.Xpf.Printing.v21.1" />
<Reference Include="DevExpress.Xpf.Grid.v21.1" />
<Reference Include="DevExpress.Xpf.LayoutControl.v21.1" />
<Reference Include="DevExpress.Xpf.Grid.v21.1.Core" />
<Reference Include="DevExpress.Xpf.Grid.v21.1.Extensions" />
<Reference Include="DevExpress.Mvvm.v21.2" />
<Reference Include="DevExpress.Printing.v21.2.Core" />
<Reference Include="DevExpress.Xpf.Core.v21.2" />
<Reference Include="DevExpress.Xpf.Printing.v21.2" />
<Reference Include="DevExpress.Xpf.Grid.v21.2" />
<Reference Include="DevExpress.Xpf.LayoutControl.v21.2" />
<Reference Include="DevExpress.Xpf.Grid.v21.2.Core" />
<Reference Include="DevExpress.Xpf.Grid.v21.2.Extensions" />
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.4.0\lib\net45\EntityFramework.dll</HintPath>
</Reference>
Expand Down
Loading