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

时间:2016-11-29 19:46:39

标签: mvvm xamarin datagrid xamarin.forms syncfusion

如果选择模式为单一,但是如果设置为多个,那么我能够绑定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中,选择模式设置为多个但我无法在xaml中获取SelectedItems,如此处所述

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

1 个答案:

答案 0 :(得分:2)

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

但是,您可以通过编写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;
}

另外,我们准备了一个样本供您参考,您可以从以下链接下载。

示例链接:http://www.syncfusion.com/downloads/support/directtrac/168321/ze/DataGridDemo352928928

此致

Divakar。