从嵌套的RepeaterView获取SelectedItem

时间:2018-07-18 10:23:35

标签: xaml xamarin xamarin.forms

我使用的DottorPagliaccius repeater view嵌套在另一个Repeaterview中。除非在顶部中继器上,否则命令RSSFeedSelectedCommand不起作用,这在理论上是合理的,但如何使它工作?

我已经研究过使用Converters和bindtoeventcommand,但看不到让它们正常工作。

<repeater:RepeaterView x:Name="MainRepeater" SeparatorHeight="25" ItemsSource={Binding Feeds}">
    <repeater:RepeaterView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Spacing="10">
                    <Label Text="{Binding Title}" TextColor="Blue" />
                    <Label Text="{Binding Description}" TextColor="Red" FontSize="12" />
                    <repeater:RepeaterView x:Name="MainRepeater2" EmptyText="No elements" ShowSeparator="true" SeparatorHeight="2" SeparatorColor="Silver" ItemsSource="{Binding Items}" SelectedItemCommand="{Binding RSSFeedSelectedCommand}">
                        <repeater`enter code here`:RepeaterView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <StackLayout Orientation="Horizontal">
                                        <Image Source="{Binding Image.Url}"></Image>
                                        <StackLayout Orientation="Vertical">
                                            <Label Text="{Binding Title}" TextColor="Black" />
                                            <Label Text="{Binding Description}" TextColor="Black" FontSize="12" />
                                        </StackLayout>
                                    </StackLayout>
                                </ViewCell>
                            </DataTemplate>
                        </repeater:RepeaterView.ItemTemplate>
                    </repeater:RepeaterView>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </repeater:RepeaterView.ItemTemplate>
</repeater:RepeaterView>

2 个答案:

答案 0 :(得分:1)

如果我的假设是正确的,则您的类结构应类似于

ViewModel
  |__List of Feeds
    |__List of Items
  |__RSSFeedSelectedCommand 

因此,内部中继器控件(DataContext)中的MainRepeater2将是Feed。因此,请尝试使用外部转发器控件的DataContext。就像

<repeater:RepeaterView x:Name="MainRepeater2" ItemsSource="{Binding Items}"
   SelectedItemCommand="{Binding DataContext.RSSFeedSelectedCommand,
                                 ElementName=MainRepeater}"/>

Xamarin

<repeater:RepeaterView x:Name="MainRepeater2" ItemsSource="{Binding Items}"
   SelectedItemCommand="{Binding DataContext.RSSFeedSelectedCommand, 
                                 Source={x:Reference MainRepeater}}"/>

答案 1 :(得分:1)

Repeater试图在Feeds中查找命令,但是ViewModel中存在该命令。

<repeater:RepeaterView x:Name="MainRepeater2" EmptyText="No elements" ShowSeparator="true" SeparatorHeight="2" SeparatorColor="Silver" ItemsSource="{Binding Items}" SelectedItemCommand="{Binding Source={x:Reference RSSPage}, Path=BindingContext.RSSFeedSelectedCommand}">

,您必须命名页面:

<ContentPage x:Name="PageName" />