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

I can't map RadGridView.AddingNewDataItem event to command in viewmodel in my Prism 6 application.

4 Answers 104 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Eugene
Top achievements
Rank 1
Eugene asked on 08 Apr 2016, 11:02 AM

Hi! I use RadGridView in one of the views of my WPF MVVM Prism 6 application. The view was created on the base of Prism UserControl (WPF). The
solution of my application was created as empty Telerik project. After I had created the solution I added here RadWindow as Shell and also Prism 6
library for WPF via NuGet. I want to map RadGridView.AddingNewDataItem event to a command in viewmodel but unfortunately I can't. Below is RadGridView' XAML snippet from view' markup:

<telerik:RadGridView Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center"  Margin="8,5,0,0" VerticalAlignment="Top" AutoGenerateColumns="False"
            IsReadOnly="True" ItemsSource="{Binding DeviceRecords}" SelectedItem="{Binding SelectedDevice}">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding DeviceName}" Header="Name"/>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding SerialNumber}" Header="Serial Number"/>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding MountingLocation}" Header="Mounting place"/>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding CompanyOwnerOfDevice}" Header="Device' owner"/>
            </telerik:RadGridView.Columns>

            <telerik:EventToCommandBehavior.EventBindings>
                <telerik:EventBinding
                    Command="{Binding HandleAddingNewRecordToDevicesGridCommand}"
                    EventName="AddingNewDataItem"
                    PassEventArgsToCommand="True"/>
            </telerik:EventToCommandBehavior.EventBindings>
</telerik:RadGridView>

Below is HandleAddingNewRecordToDevicesGridCommand command that is in viewmodel:

public DelegateCommand<object> HandleAddingNewRecordToDevicesGridCommand { get; private set; }

private void handleAddingNewRecordToDevicesGrid(object parameter)
{
            // Get RadGridView that is sender.
            Telerik.Windows.Controls.RadGridView gridView =
                ((parameter as GridViewAddingNewEventArgs).OwnerGridViewItemsControl) as Telerik.Windows.Controls.RadGridView;
            // Get new item inserted in the RadGridView.
            Telerik.Windows.Controls.GridViewDataColumn newItem =
                ((parameter as GridViewAddingNewEventArgs).NewObject) as Telerik.Windows.Controls.GridViewDataColumn;
            // Prepair to select this new item in the RadGridView.
            Telerik.Windows.Controls.GridViewDataColumn[] aItem = { newItem };
            System.Collections.IEnumerable selectedItem = aItem;
            // Select the new item in the RadGridView.
            gridView.Select(selectedItem);
}

this.HandleAddingNewRecordToDevicesGridCommand = new DelegateCommand<object>(this.handleAddingNewRecordToDevicesGrid); // (in viewmodel constructor)

I'm in need of abovementioned code for selecting new inserted row in the RadGridView. So after new row has been inserted into RadGridView then this row is selected in the RadGridView. But HandleAddingNewRecordToDevicesGridCommand command doesn't fire at all! Why? Please help me.

4 Answers, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 08 Apr 2016, 01:02 PM
Hello Eugene,

I'm attaching a sample project where I tried to reproduce your scenario. However, the command is successfully bound to the AddingNewDataItem event on my side.

Could you please have a look at the attached project and let me know if I'm missing something? 

Regards,
Dilyan Traykov
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Eugene
Top achievements
Rank 1
answered on 12 Apr 2016, 07:47 AM
Hi, Dilyan. I appreciate your help very. Thank you very much for it. BUT. In your example you initiate insertion record to RadGridView by button click by calling of 'this.clubsGrid.BeginInsert()' method in button click handler and so your RadGridView is not ReadOnly. In my application RadGridView is ReadOnly and I insert new element not in RadGridView directly but in ObservableCollection to which RadGridView is bound. So RadGridView is getting new record not directly but via ObservableCollection to which it is bound. So, will 'AdingNewDataItem' event of RadGridView fire in my situation (where insertion new element in RadGridView is executed in indirect manner) or not?
0
Accepted
Dilyan Traykov
Telerik team
answered on 12 Apr 2016, 11:22 AM
Hello Eugene,

Unfortunately, the AdingNewDataItem will not get fired if your RadGridView is Read-Only as in the approach, you've described.

If you simply want to select the newly-created item, however, you can do something like this:

public MyViewModel()
{
    this.AddCommand = new DelegateCommand<object>(this.Add);
}
 
public DelegateCommand<object> AddCommand { get; private set; }
 
public void Add(object parameter)
{
    var club = new Club();
    this.Clubs.Add(club);
    this.SelectedItem = club;
}
 
public object SelectedItem
{
    get { return this.selectedItem; }
    set
    {
        if (value != this.selectedItem)
        {
            this.selectedItem = value;
            this.OnPropertyChanged("SelectedItem");
        }
    }
}

<telerik:RadGridView SelectedItem="{Binding SelectedItem}" />
<Button Command="{Binding AddCommand}" Content="Add" />

If that is not the case and you would also like to focus the new item, I'm afraid there's no MVVM-friendly way to achieve this. You will need to add this to your code behind:

var items = this.clubsGrid.ItemsSource as ObservableCollection<Club>;
var item = new Club();
items.Add(item);
this.clubsGrid.SelectedItem = item;
this.clubsGrid.Focus();
 
Please let me know if this approach would work for you.

Regards,
Dilyan Traykov
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Eugene
Top achievements
Rank 1
answered on 13 Apr 2016, 05:26 AM
Thank you, Dilyan. Your help is very significant.
Tags
GridView
Asked by
Eugene
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Eugene
Top achievements
Rank 1
Share this question
or