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

5 posts, 1 answers
  1. Eugene
    Eugene avatar
    83 posts
    Member since:
    Jan 2016

    Posted 08 Apr Link to this post

    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.

  2. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    371 posts

    Posted 08 Apr Link to this post

    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.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Eugene
    Eugene avatar
    83 posts
    Member since:
    Jan 2016

    Posted 12 Apr in reply to Dilyan Traykov Link to this post

    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?
  5. Answer
    Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    371 posts

    Posted 12 Apr Link to this post

    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.
  6. Eugene
    Eugene avatar
    83 posts
    Member since:
    Jan 2016

    Posted 13 Apr in reply to Dilyan Traykov Link to this post

    Thank you, Dilyan. Your help is very significant.
Back to Top
UI for WPF is Visual Studio 2017 Ready