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. Image for the cookie policy date

Binding SelectedItems doesnt work

I am trying to achieve selection with mode multiple and binding SelectedItems in viewmodel as below. But it is not fired or bound. The same way I can achieve single and SelectedItem binding. Is there anything which I am doing wrong or missing here?

Binding between xaml and viewmodel is just fine. I am able to display all items but just doesnt work binding SelectedItems 

In Xaml;

  <sfgrid:SfDataGrid  ScrollingMode="PixelLine"  x:Name="Grid"
                      AutoGenerateColumns="False"
                      AllowSorting="True"
                         IsEnabled="True"
                      ColumnSizer="Star"
                      AllowSwiping="True"                   
                      MaxSwipeOffset="150"
                      GridTapped="dataGrid_GridTapped"
                      SelectionMode="Multiple"          
                       AutoExpandGroups="True"
                        AllowGroupExpandCollapse="True"
                      RowHeight="100" ItemsSource="{Binding Items}"
           SelectedItems="{Binding SelectedItems , Mode=OneWayToSource}"  VerticalOptions="FillAndExpand">        

In ViewModel;

    public ObservableCollection<Item> SelectedItems { get;  }

4 Replies

AN Ashok N Syncfusion Team December 14, 2016 07:38 AM UTC

Hi Emil,    

Thanks for contacting Syncfusion support.   

We have checked your query. In SfDataGrid, it is not possible to bind the SfDataGrid.SelectedItems property to the view model as like SelectedItem property since we can only get the selected items in SfDataGrid. Hence, you will not be able to bind the values in Xaml for SelectedItems property.  
  
However, you can achieve your requirement by writing behavior for SfDataGrid which will not affect the MVVM pattern. Please refer the below code snippet.  
  
<sfGrid:SfDataGrid x:Name="dataGrid"  
                   AutoGenerateColumns="True"  
                   ItemsSource="{Binding OrdersInfo}"  
                   SelectionMode="Multiple">  
  
    <b:Interaction.Behaviors>  
        <b:BehaviorCollection>  
            <b:EventToCommand Command="{Binding SelectionCommand}"  
                              CommandParameter="{x:Reference Name=dataGrid}"  
                              EventName="SelectionChanged" />  
        </b:BehaviorCollection>  
    </b:Interaction.Behaviors>  
</sfGrid:SfDataGrid>  
  
// In ViewModel.cs  
public ViewModel()  
{  
     selectionCommand = new Command<SfDataGrid>(onSelectionChanged);  
     selectedItems = new ObservableCollection<object>();  
}  
  
private Command<SfDataGrid> selectionCommand;  
public Command<SfDataGrid> SelectionCommand  
{  
    get { return selectionCommand; }  
    set { selectionCommand = value; }  
}  
  
private ObservableCollection<object> selectedItems;  
  
public ObservableCollection<object> SelectedItems  
{  
    get { return selectedItems; }  
    set { selectedItems = value; }  
}  
  
private void onSelectionChanged(SfDataGrid obj)  
{  
    //you can get the selected items in the datagrid  
    selectedItems = obj.SelectedItems;  
}  
  
Also, we have prepared a sample for your reference and you can download the same from the below link.  
  
Regards,  
Ashok  



EM Emil December 14, 2016 02:57 PM UTC

Hi Ashok,

thank you this is working but i wish that you consider this as bindable in 2017 releases :) It is misleading. 

Best Regards,

Emil 


AN Ashok N Syncfusion Team December 15, 2016 05:23 AM UTC

Hi Emil, 

We have checked your query and SelectedItems is Collection and it does not have setter property. So, we do not provide the bindable support  for this. 

Regards, 
Ashok 



AN Ashok N Syncfusion Team December 15, 2016 05:24 AM UTC

Hi Emil, 

We have checked your query and SelectedItems is Collection and it does not have setter property. So, we do not provide the bindable support  for this. 

Regards, 
Ashok 


Loader.
Up arrow icon