2016-11-29 60 views
0

我能够绑定SelectedItem如果选择模式是单一的,但如果它设置为多个,那么你如何绑定它?如何在mvvm中绑定多个选定项目的syncfusion xamarin表单datagrid?

这里是我试过单一选择模式

<sync:SfDataGrid Grid.Row="1" AutoGenerateColumns="False" AllowSorting="True" 
             AllowGroupExpandCollapse="True" AutoExpandGroups="True" 
            SelectionMode="Multiple" ColumnSizer="Star" 
            ItemsSource="{Binding LstItems}" 
             SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}" 
            > 
          <sync:SfDataGrid.Columns> 
           <sync:GridTextColumn HeaderText="Name" MappingName="Name" /> 
           <sync:GridTextColumn HeaderText="MRP" MappingName="MRP"/> 
           <sync:GridTextColumn HeaderText="Category" MappingName="Category" Width="0"/> 
          </sync:SfDataGrid.Columns> 
          <sync:SfDataGrid.GroupColumnDescriptions> 
           <sync:GroupColumnDescription ColumnName="Category"/> 
          </sync:SfDataGrid.GroupColumnDescriptions> 
         </sync:SfDataGrid> 

在上面的XAML中,选择模式设定为多,但我无法得到在XAML的SelectedItems这里提到

https://help.syncfusion.com/xamarin/sfdatagrid/selection

回答

1

在SfDataGrid中,由于我们只能得到SfDataGrid中的选定项,因此不可能将SfDataGrid.SelectedItems属性绑定到视图模型,就像SelectedItem属性一样。因此,您将无法为SelectedItems属性绑定XAML中的值。

但是,您可以通过编写不会影响MVVM模式的SfDataGrid行为来实现您的要求。请参阅下面的代码片段。

<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; 
} 

此外,我们准备了一个样本供您参考,您可以从下面的链接下载相同的样本。

样本链接:http://www.syncfusion.com/downloads/support/directtrac/168321/ze/DataGridDemo352928928

问候,

Divakar。

+0

你不能在nuget中找到Xamarin.Behaviors,而是使用Corcav.Behaviors(创建Xamarin.Behaviors的开发者)。 –