为什么我的 IsVisible 绑定不适用于 StackLayout ?赛马林 iOS

Why does my IsVisible Binding Not Work with a StackLayout ? Xamarin iOS

我有下面的 XAML 结构,它使用一个集合视图,它使用一个名为 'users' 的列表作为其 ItemSource。我有一个绑定 属性,它控制项目是否可见:

 <StackLayout Orientation="Horizontal">
 <Frame IsVisible="{Binding isVisible}" HasShadow="True" BackgroundColor="White" Padding="0">

问题是,除非我删除框架周围的 StackLayout,否则 IsVisible 绑定不起作用。有谁知道为什么绑定可以在没有 StackLayout 的情况下工作,但不能与?

<CollectionView Grid.Row="1" x:Name="users" IsGrouped="True" SelectionMode="Single">
            <CollectionView.GroupHeaderTemplate>
                <DataTemplate>
                    <StackLayout Orientation="Horizontal" MinimumHeightRequest="200" BackgroundColor="{Binding tint_colour}">
                        <Image Source="{Binding school_image}" WidthRequest="120" HeightRequest="100"/>
                        <Label Text="{Binding organisation_title}"                      
                                    TextColor="{Binding font_colour}"
                                    FontSize="Large"
                                    FontAttributes="Bold"
                                    VerticalTextAlignment="Center"></Label>
                        <StackLayout.GestureRecognizers>
                            <TapGestureRecognizer Tapped="HeaderTapped" CommandParameter="{Binding organisation_title}"></TapGestureRecognizer>
                        </StackLayout.GestureRecognizers>
                    </StackLayout>
                </DataTemplate>
            </CollectionView.GroupHeaderTemplate>
            <CollectionView.ItemsLayout>
                <LinearItemsLayout Orientation="Vertical" ItemSpacing="1"/>
            </CollectionView.ItemsLayout>
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <StackLayout Orientation="Horizontal">
                        <Frame IsVisible="{Binding isVisible}" HasShadow="True" BackgroundColor="White" Padding="0">
                            <Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
                                <behaviors:Expander x:Name="MainExpander" CollapseAnimationLength="500" IsExpanded="False" IsVisible="True" >
                                    <behaviors:Expander.Header>
                                        <Grid HorizontalOptions="FillAndExpand">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="100"/>
                                                <ColumnDefinition Width="10"/>
                                            </Grid.ColumnDefinitions>
                                            <Frame HeightRequest="40" WidthRequest="40" CornerRadius="20" HorizontalOptions="Start" VerticalOptions="Center" Margin="20" Padding="0" BackgroundColor="Maroon">
                                                <Label Text="{Binding student_initial}" TextColor="White" HorizontalOptions="Center" VerticalOptions="Center" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" />
                                            </Frame>
                                            <StackLayout Grid.Column="2" HorizontalOptions="StartAndExpand" VerticalOptions="Center" Margin="20">
                                                <Label IsVisible="{Binding isVisible}" x:Name="StudentName" Text="{Binding student_fullname}"></Label>
                                                <Label x:Name="StudentID" IsVisible="false" Text="{Binding student_unique_id}"></Label>
                                            </StackLayout>
                                        </Grid>
                                    </behaviors:Expander.Header>
                                    <Grid RowSpacing="0" HorizontalOptions="FillAndExpand" HeightRequest="240" VerticalOptions="FillAndExpand">
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="*"/>
                                            <RowDefinition Height="*"/>
                                            <RowDefinition Height="*"/>
                                        </Grid.RowDefinitions>
                                        <Button Grid.Row="0" Text="Messages" Clicked="Button_Clicked"></Button>
                                        <Button x:Name="btnTopUp" Grid.Row="1" Text="Quick Topup" Clicked="Button_Clicked" IsVisible="{Binding topup_product_id, Converter={StaticResource IsNotNullOrEmptyConverter}}"></Button>
                                        <Button Grid.Row="2" Text="Payments" Clicked="Button_Clicked"></Button>
                                    </Grid>
                                    <!-- TODO: Look at adding a balance for childrens topups? -->
                                </behaviors:Expander>
                            </Grid>
                        </Frame>
                    </StackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>

我对此进行了测试,它工作正常,这里是 xaml 和代码隐藏:

xmal:

<StackLayout>
        <CollectionView x:Name="mycol">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <StackLayout >
                        <Frame BackgroundColor="Red" IsVisible="{Binding isvisible}">
                         <Label Text="{Binding Name}"/>
                            </Frame>
                     </StackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </StackLayout>

代码隐藏:

public partial class CollectionTest : ContentPage
{
    ObservableCollection<People> peoples = new ObservableCollection<People> {
    new People{ Name="Adam",isvisible=true},
    new People{ Name="Bob",isvisible=true},
    new People {Name="Adam2",isvisible=false },
    new People{ Name="Bob2",isvisible=true} };
    public CollectionTest()
    {
        InitializeComponent();
        mycol.ItemsSource = peoples;
    }