如何在mvvm中绑定syncfusion xamarin forms datagrid的多个选定项?

如何在mvvm中绑定syncfusion xamarin forms datagrid的多个选定项?,mvvm,xamarin,datagrid,xamarin.forms,syncfusion,Mvvm,Xamarin,Datagrid,Xamarin.forms,Syncfusion,如果选择模式是单模式,我可以绑定SelectedItem,但如果设置为多模式,那么如何绑定它 以下是我尝试的单选模式 <sync:SfDataGrid Grid.Row="1" AutoGenerateColumns="False" AllowSorting="True" AllowGroupExpandCollapse="True" AutoExpandGroups="True"

如果选择模式是单模式,我可以绑定SelectedItem,但如果设置为多模式,那么如何绑定它

以下是我尝试的单选模式

<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中,选择模式被设置为multiple,但我无法在xaml中获得SelectedItems,如这里所述


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

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

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

//在ViewModel.cs中
公共视图模型()
{
selectionCommand=新命令(onSelectionChanged);
selectedItems=新的ObservableCollection();
}
私有命令选择命令;
公共命令选择命令
{
获取{return selectionCommand;}
设置{selectionCommand=value;}
}
私有可观测集合selectedItems;
公共可观测集合SelectedItems
{
获取{return selectedItems;}
设置{selectedItems=value;}
}
已更改选择上的私有无效(SfDataGrid obj)
{
//您可以在datagrid中获取所选项目
selectedItems=obj.selectedItems;
}
此外,我们还准备了一个样本供您参考,您可以从下面的链接下载

示例链接:

问候,


Divakar.

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

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

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

//在ViewModel.cs中
公共视图模型()
{
selectionCommand=新命令(onSelectionChanged);
selectedItems=新的ObservableCollection();
}
私有命令选择命令;
公共命令选择命令
{
获取{return selectionCommand;}
设置{selectionCommand=value;}
}
私有可观测集合selectedItems;
公共可观测集合SelectedItems
{
获取{return selectedItems;}
设置{selectedItems=value;}
}
已更改选择上的私有无效(SfDataGrid obj)
{
//您可以在datagrid中获取所选项目
selectedItems=obj.selectedItems;
}
此外,我们还准备了一个样本供您参考,您可以从下面的链接下载

示例链接:

问候,


Divakar.

在nuget中找不到Xamarin.Behaviors,请使用Corcav.Behaviors(与创建Xamarin.Behaviors的开发人员相同)。在nuget中找不到Xamarin.Behaviors,请使用Corcav.Behaviors(与创建Xamarin.Behaviors的开发人员相同)。