We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

Programmatically set binded SelectedItem does not change SfDataGrid.SelectedItems and UI

Thread ID:

Created:

Updated:

Platform:

Replies:

123403 Mar 15,2016 05:21 AM Mar 17,2016 12:27 AM WPF 6
loading
Tags: SfDataGrid
bruce
Asked On March 15, 2016 05:21 AM

Hi, my SfDataGrid.SelectedItem is binded (TwoWay) to a property in ViewModel.
After changing ViewModel.SelectedItem, SfDataGrid does not select the selected item on-screen. SfDataGrid.SelectedItems is not updated too.
Is this a bug?

In View:
<Syncfusion:SfDataGrid x:Name="sfdatagrid"
                               Grid.Row="0"
                               ColumnSizer="Star"
                               ItemsSource="{Binding EmployeeDetails}"
                               NavigationMode="Cell"
                               SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
                               SelectionUnit="Row">

In ViewModel:
        private object selecteditem;
        public object SelectedItem {
            get  { return selecteditem; }
            set {
                selecteditem = value;
                RaisePropertyChanged("SelectedItem");
            }
        }
        NewRow() {
            employeelist.Add(new Model());
            selecteditem = employeelist[employeelist.Count-1];
            // new item is not selected in UI, and SfDataGrid.SelectedItems does not have this item in it too.
        }


Thanks!

bruce
Replied On March 15, 2016 09:19 PM

I've attached a sample which was modified from a sample downloaded from a thread in this forum.
I added a "New Row" button which demonstrates the issue.
Thanks !

Attachment: WPF_508e1d95.rar

bruce
Replied On March 15, 2016 09:24 PM

My intention is to allow the ViewModel to control the SelectedItem, without depending on View, for the sake of MVVM separation and Unit Test.
For example, in ViewModel.NewRow() command, it will add a new row to a collection and set the SelectedItem to the new row. The row in the grid should be selected and SelectedItems should include this item too.


Jayapradha S [Syncfusion]
Replied On March 16, 2016 06:18 AM

Hi  CHLoh,

Thank you for contacting Syncfusion Support.

We have analyzed your query with your updated sample. You didn’t implement the interface INotifyPropertyChanged in ViewModel class and so new row will not be selected. We have modified the sample and this new selected item has been added in SfDataGrid.SelectedItems.

Please find the modified sample from the following location,
Sample : http://www.syncfusion.com/downloads/support/forum/123403/ze/WPF-952906245

Regards,
Jayapradha

bruce
Replied On March 16, 2016 08:57 PM

Oh thanks! I feel so silly :)

Jayapradha S [Syncfusion]
Replied On March 17, 2016 12:24 AM

Hi Will,

Thank you for your update.

Please let us know if you require any other assistance.

Regards,
Jayapradha


Jayapradha S [Syncfusion]
Replied On March 17, 2016 12:27 AM

Hi CHLoh,


Thank you for your update. 

Please let us know if you require any other assistance.

Regards,
Jayapradha


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;