如何从RadListView获取所选项目?

时间:2017-11-26 08:04:51

标签: android xamarin xamarin.ios telerik

我的RadListView绑定到一个对象列表。我的XAML看起来像这样:

<telerikDataControls:RadListView x:Name="ListView" VerticalOptions="FillAndExpand" SelectionMode="Single" ItemTapped="ListView_OnCellTapped">               

这是我的事件方法:

void ListView_OnCellTapped(object sender, EventArgs e)
{
  var obj = ListView.SelectedItem;
}             

但是,SelectedItem始终返回null。我怀疑我需要从EventArgs中获取所选对象,但我不确定如何不能直接访问e中包含的Items集合。有什么想法吗?

编辑1:这是我的XAML:

<telerikDataControls:RadListView x:Name="ListViewMachines" HeightRequest="357" SelectionMode="Single" ItemTapped="ListViewMachines_OnItemTapped">
    <telerikDataControls:RadListView.ItemTemplate>
        <DataTemplate>
            <telerikListView:ListViewTemplateCell Tapped="Cell_OnTapped">
                <telerikListView:ListViewTemplateCell.View>

                    <Grid VerticalOptions="Center" HorizontalOptions="FillAndExpand" Padding="16, 0, 0, 0">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="30"></RowDefinition>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Grid Row="0" Column="0">
                            <iconize:IconImage IconSize="25" IconColor="Green" Icon="fa-cogs"></iconize:IconImage>
                        </Grid>
                        <Grid Row="0" Column="1">
                            <Label Text="{Binding MachineName}" FontSize="Medium" />
                        </Grid>
                    </Grid>

                </telerikListView:ListViewTemplateCell.View>
            </telerikListView:ListViewTemplateCell>
        </DataTemplate>
    </telerikDataControls:RadListView.ItemTemplate>
    <telerikDataControls:RadListView.GroupDescriptors>
        <telerikListView:PropertyGroupDescriptor PropertyName="FunctionalLocationName"/>
    </telerikDataControls:RadListView.GroupDescriptors>
    <telerikDataControls:RadListView.GroupHeaderTemplate>
        <DataTemplate>
            <Grid BackgroundColor="#C1C1C1">
                <Label Text="{Binding }" TextColor="#303030" FontSize="Large" HorizontalOptions="Center"/>
            </Grid>
        </DataTemplate>
    </telerikDataControls:RadListView.GroupHeaderTemplate>
</telerikDataControls:RadListView>

我尝试将ItemTapped和SelectionChanged添加到telerikDataControls:RadListView,但都没有返回所选对象。

4 个答案:

答案 0 :(得分:1)

获取GridView SelectedCells,如下所示:

if (this.radGridView1.SelectedCells.Count > 0)
{
     GridViewSelectedCellsCollection selectedCells = this.radGridView1.SelectedCells;
}

答案 1 :(得分:1)

  

但是,SelectedItem始终返回null。

根据您的说明,您可能需要在XAML中使用ItemSelected="OnSelection"而不是ItemTapped

根据Xamarin.Forms ListView Sample: Interactivity,所选行动可以是这样的:

    void OnSelection (object sender, SelectedItemChangedEventArgs e)
    {
        if (e.SelectedItem == null) {
            return; //ItemSelected is called on deselection, which results in SelectedItem being set to null
        }
        DisplayAlert ("Item Selected", e.SelectedItem.ToString (), "Ok");
        //comment out if you want to keep selections
        ListView lst = (ListView)sender;
        lst.SelectedItem = null;
    }

因此,所选项目可以作为示例代码(link)获取:

e.SelectedItem

请注意,使用SelectedItemChangedEventArgs作为参数类型而不是EventArgs。然后,您可以使用e.SelectedItem直接获取所选项目。

答案 2 :(得分:0)

感谢你们的片段,但我设法找到了如何使用ItemTapped事件做到这一点:

private void ListView_OnItemTapped(object sender, ItemTapEventArgs e)
{
  var obj = e.Item as MyObjectType;
}

答案 3 :(得分:0)

这是我的XAML代码

<telerikDataControls:RadListView x:Name="clientView" ItemTapped="OnItemTapped">
            <telerikDataControls:RadListView.ItemTemplate>
                <DataTemplate>
                    <telerikListView:ListViewTemplateCell>
                        <telerikListView:ListViewTemplateCell.View>
                            <Grid>
                                <Label Margin="10" Text="{Binding Client}" />
                            </Grid>
                        </telerikListView:ListViewTemplateCell.View>
                    </telerikListView:ListViewTemplateCell>
                </DataTemplate>
            </telerikDataControls:RadListView.ItemTemplate>
        </telerikDataControls:RadListView>

然后在C#中进行选择

        async private void OnItemTapped(object sender, ItemTapEventArgs e)
    {
        Client selectedClient = e.Item as Client;

        //open details page
        var masterdetailpage = Application.Current.MainPage as MasterDetailPage;
        var clientDetails = new ClientDetails(); //page
        clientDetails.BindingContext = selectedClient; //passing data

        await masterdetailpage.Detail.Navigation.PushAsync(clientDetails);

    }