This is a migrated thread and some comments may be shown as answers.

How to user "CellEditEnded" event using MVVM?

4 Answers 1572 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Psyduck
Top achievements
Rank 5
Bronze
Bronze
Bronze
Psyduck asked on 22 Mar 2021, 01:20 AM

Hello.

 

I want to use CellEditEnded event using MVVM.

It's a summary source.

<Button Content="Load"
        Command="{Binding OnClickLoadCommand}"
        CommandParameter="{Binding ElementName=gridViewSharedParameter}"/>
<telerik:RadGridView x:Name="gridViewSharedParameter"
                     ItemsSource="{Binding SharedParameterGroupItem.SharedParameterName}"
                     SelectedItem="{Binding SharedParameterSelectedItem, Mode=TwoWay}"
                     CurrentItem="{Binding SharedParameterCurrentItem, Mode=TwoWay}"
                     >
</telerik:RadGridView>
<telerik:EventToCommandBehavior.EventBindings>
    <telerik:EventBinding EventName="CellEditEnded"
                                      Command="{Binding OnCelleditEndedTextCommand}"
                                      CommandParameter="{Binding ElementName=gridViewSharedParameter}"/>
</telerik:EventToCommandBehavior.EventBindings>

 

I found 2 ways.

The first is how to trigger an event in MVVM by using a button.

1. ViewModel.cs (using button in CellEditEnded event)
private void OnClickLoad(object o)
{
    (o as RadGridView).CellEditEnded += ViewModel_CellEditEnded;
}
 
 private void ReadParamFromExcelViewModel_CellEditEnded(object sender, GridViewCellEditEndedEventArgs e)
{
   . . . . . . . ~~~~~
}

 

 

The second uses Telerik EventBinding.

2. ViewModel.cs (Using Xaml telerik:EventToCommandBehavior.EventBindings)
 
private void OnCelleditEndedText(object o)
{
  var objectItem   = o as Telerik.Windows.Controls.RadGridView; // 
  var currentItem  = SharedParameterCurrentItem;
  var selectedItem = SharedParameterSelectedItem;
}

 

 

I do not prefer the first method.

I want to use it in the second way, can I get the GridViewCellEditEndedEventArgs?

I want to control newdata, olddata, and editaction through GridViewCellEditEndedEventArgs.

 

+)  What is the difference between CurrentItem and SelectedItem? It seems to receive the same value.

 

Thanks.

4 Answers, 1 is accepted

Sort by
0
Psyduck
Top achievements
Rank 5
Bronze
Bronze
Bronze
answered on 24 Mar 2021, 01:57 AM

Hello.

In the second way, you know how to get eventArgs.

Among the binding properties, PassEventArgsToCommand="True" was used.

However, if you use this, you can't do CommandParameter.

 

private void OnCelleditEndedText(object o)
{
  var objectItem   = o as GridViewCellEditEndedEventArgs; //  PassEventArgsToCommand="True"
  var currentItem  = SharedParameterCurrentItem;
  var selectedItem = SharedParameterSelectedItem;
}

 

I want to use both (object sender, GridView CellEditEndedEventArgs e) parameter sender, e. Is there a way?

Please also ask for the difference between CurrentItem and SelectedItem.

 

Thanks.

 

 

0
Martin Ivanov
Telerik team
answered on 24 Mar 2021, 11:41 AM

Hello KIM,

Yes, the PassEventArgsToCommand property of the EventBinding is used to provide the event arguments in the command execute handler. However, you cannot use both - the CommandParameter and the PassEventArgsToCommand. However, in your case you can get the RadGridView instance (used as CommandParameter in you original code snippet), via the event arguments.

var objectItem   = o as GridViewCellEditEndedEventArgs;
GridViewDataControl gridView = objectItem.Cell.ParentDataControl; 
// note that GridViewDataControl is the base class of RadGridView, so if you prefer, you can cast it to RadGridView

For the difference between CurrentItem and SelectedItem. The SelectedItem is the data item selected in the UI. The CurrentItem points to the same property of the ICollectionView used internally with RadGridView (the Items collection). The ICollectionView is used in order to support grouping, filtering and sorting in the control. Read more about the ICollectionView's CurrentItem in MSDN. And for the CurrentItem support in RadGridView in the Telerik help documentation.

Regards,
Martin Ivanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Psyduck
Top achievements
Rank 5
Bronze
Bronze
Bronze
answered on 25 Mar 2021, 01:31 AM

Hello.

It was a great help. Thanks.

This is currently specific to RadGridView.

 

If I use CellPropertyChanged on the SparedSheet, how can I extract the object sender?

The event CellPropertyChangedEventArgs has "CellRange" and "Property".

Property has ICellValue as a base class, but sender has Cells class.

The final purpose here is to extract the Sender from EventArgs and get the Cell Value.

 

The above is what I need now again, and is there a way to find it myself if I need it later?

Thanks.

0
Martin Ivanov
Telerik team
answered on 25 Mar 2021, 02:01 PM

Hello KIM,

There is no information about the RadSpreadsheet object in the CellPropertyChangedEventArgs. In this case, you can consider using code-behind instead of the EventToCommandBehavior, since when using the behavior you cannot provide both the event arguments and a CommandParameter. In this case, you can do something like this:

private void Cells_CellPropertyChanged(object sender, Telerik.Windows.Documents.Spreadsheet.PropertySystem.CellPropertyChangedEventArgs e)
{
	var commandParameter = new object[2] { this.readsporead, e };
	this.myViewModel.CellPropertyChangedCommand.Execute(commandParameter);
}

Regards,
Martin Ivanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
GridView
Asked by
Psyduck
Top achievements
Rank 5
Bronze
Bronze
Bronze
Answers by
Psyduck
Top achievements
Rank 5
Bronze
Bronze
Bronze
Martin Ivanov
Telerik team
Share this question
or