简体   繁体   English

Xamarin Forms iOS中的嵌套Bindabale布局ItemSource

[英]Nested Bindabale Layout ItemSource in Xamarin Forms iOS

I have a StackLayout with Bindable Layout ItemSource set, the stacklayout's datatemplate contains an Expander View (available in Xamarin Forms 4.7), my requirement is on clicking Expander header, dynamic data should be loaded and displayed in Expander body, for that, I'm using a pancakeview to which another Bindable Layout ItemSource is set and this is handled in code behind. I have a StackLayout with Bindable Layout ItemSource set, the stacklayout's datatemplate contains an Expander View (available in Xamarin Forms 4.7), my requirement is on clicking Expander header, dynamic data should be loaded and displayed in Expander body, for that, I'm使用设置了另一个可绑定布局 ItemSource 的 pancakeview,这在后面的代码中处理。

Basically what I'm trying to achieve is there is a list, which gets populated on the page open, On clicking any item it should expand and show additional data, which is loaded from server on Expander header click.基本上我想要实现的是有一个列表,它会在打开的页面上填充,单击任何项目时它应该展开并显示其他数据,这些数据是从服务器加载的 Expander header 点击。

What is the best way to achieve this?实现这一目标的最佳方法是什么? below is what I have done so far:以下是我到目前为止所做的:

<StackLayout x:Name="StudentsStack" BindableLayout.ItemsSource="{Binding StudentData}" Margin="10" 
                             HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" >
<BindableLayout.ItemTemplate>
    <DataTemplate>
        <Expander Tapped="StudentsClicked" ExpandAnimationEasing="{x:Static Easing.CubicIn}" ExpandAnimationLength="200" CollapseAnimationEasing="{x:Static Easing.CubicOut}" CollapseAnimationLength="200" HorizontalOptions="FillAndExpand">
            <Expander.Header>
                <Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" RowDefinitions="*" ColumnDefinitions="*">
                    <local:MarqueeLabel FontAttributes="Bold" FontSize="20" HorizontalOptions="Start" Text="{Binding Name}" TextColor="Black" XAlign="Start" FontFamily="googlesansbold"/>
                    <Label IsVisible="False" Text="{Binding Id}"/>
                </Grid>
            </Expander.Header>
            <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand">
                <pv:PancakeView BindableLayout.ItemsSource="{Binding StudentDetails}" HeightRequest="30" Margin="5" Padding="5" HasShadow="False"  HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" CornerRadius="5"  BackgroundColor="White">
                    <pv:PancakeView.Shadow>
                        <pv:DropShadow Opacity="0.1" />
                    </pv:PancakeView.Shadow>
                    <BindableLayout.ItemTemplate>
                        <DataTemplate>
                            <Grid Margin="0" ColumnDefinitions="*,*" RowDefinitions="*" HorizontalOptions="FillAndExpand">
                                <Label Grid.Row="0" Grid.Column="0" FontAttributes="Bold" FontSize="14" HorizontalOptions="Center" Text="{Binding rank}" TextColor="Black" XAlign="Start" FontFamily="googlesansbold" VerticalTextAlignment="Center" Padding="5,0,0,5"/>
                                <Label Grid.Row="0" Grid.Column="1" FontAttributes="Bold" FontSize="14" HorizontalOptions="Center" Text="{Binding address}" TextColor="Green" XAlign="Start" FontFamily="googlesansbold" VerticalTextAlignment="Center" Padding="5,0,0,5"/>
                            </Grid>
                        </DataTemplate>
                    </BindableLayout.ItemTemplate>
                </pv:PancakeView>
            </StackLayout>
        </Expander>
    </DataTemplate>
</BindableLayout.ItemTemplate>

In the code behind在后面的代码中

public ObservableCollection<Student> StudentData{ get; } = new ObservableCollection<Student>();
public ObservableCollection<Detail> StudentDetails{ get; } = new ObservableCollection<Detail>();

and inside page constructor, I'm doing this在页面构造函数中,我正在这样做

BindingContext = this;

Binding is working fine for "StudentData" list is being populated, but what is the best approach to generate additional data on expander expanded?正在填充“StudentData”列表的绑定工作正常,但是在展开的扩展器上生成额外数据的最佳方法是什么?

You could use List in List.您可以在列表中使用列表。

in class Student在 class 学生

public class Student
    {
        public ObservableCollection<Detail> StudentDetails { get; } 

        public Student (ObservableCollection<Detail> studentDetails)
        {
            StudentDetails = studentDetails;
        }

        //...other properties
    }

And in code behind在后面的代码中

StudentData = new ObservableCollection<Student>(){

 new Student(new ObservableCollection<Detail>()){//...other properties},
 //...

};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM