【问题标题】:Nested Bindabale Layout ItemSource in Xamarin Forms iOSXamarin Forms iOS 中的嵌套 Bindablelayout ItemSsource
【发布时间】:2023-04-10 19:00:01
【问题描述】:

我有一个设置了可绑定布局 ItemSource 的 StackLayout,stacklayout 的数据模板包含一个 Expander 视图(在 Xamarin Forms 4.7 中可用),我的要求是单击 Expander 标题,动态数据应加载并显示在 Expander 正文中,为此,我正在使用一个 pancakeview,它设置了另一个 Bindable Layout ItemSource,这在后面的代码中处理。

基本上我想要实现的是有一个列表,它会在打开的页面上填充,单击任何项​​目时它应该展开并显示其他数据,这些数据是在 Expander 标题单击时从服务器加载的。

实现这一目标的最佳方法是什么?以下是我到目前为止所做的:

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

在后面的代码中

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

在页面构造函数中,我正在这样做

BindingContext = this;

对于正在填充的“StudentData”列表,绑定工作正常,但是在展开的扩展器上生成额外数据的最佳方法是什么?

【问题讨论】:

    标签: c# ios forms xamarin


    【解决方案1】:

    你可以在 List 中使用 List 。

    在班学生

    public class Student
        {
            public ObservableCollection<Detail> StudentDetails { get; } 
    
            public Student (ObservableCollection<Detail> studentDetails)
            {
                StudentDetails = studentDetails;
            }
    
            //...other properties
        }
    

    在后面的代码中

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

    【讨论】:

    • 谢谢这是一个简单的方法,即使在遵循这个之后我在 XAML 中得到“无效的 Cast”异常,然后我将 PancakeView 更改为 Stacklayout。现在工作正常。
    猜你喜欢
    • 2017-01-05
    • 2021-07-07
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    • 2018-08-20
    相关资源
    最近更新 更多