如果绑定数组中没有项目,则隐藏Xamarin Forms StackLayout

问题描述 投票:0回答:1

在我的Xamarin Forms应用程序中,我有一个CarouselView可以在消息中滑动。有些邮件带有附件。当“附件”列表中没有任何项目时,我想隐藏StackLayout annexsLayout。

我曾尝试与DataTriggers玩,但没有用。

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:MyApp.Helpers" x:Class="MyApp.MailboxConversationPage" Title="Messages">
    <ContentPage.Content>
        <AbsoluteLayout x:Name="absLayout" VerticalOptions="FillAndExpand">
            <StackLayout HorizontalOptions="Fill" HeightRequest="{Binding Path=Height, Source={x:Reference absLayout}}" WidthRequest="{Binding Path=Width, Source={x:Reference absLayout}}">
                <StackLayout VerticalOptions="Start" Padding="10,10,10,5">
                  <Label x:Name="Subject" Text="Subject" FontSize="Large" FontAttributes="Bold" />
                </StackLayout>
                <StackLayout x:Name="carouselViewLayout" VerticalOptions="FillAndExpand">
                    <ContentView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" HeightRequest="{Binding Path=Height, Source={x:Reference carouselViewLayout}}" WidthRequest="{Binding Path=Width, Source={x:Reference carouselViewLayout}}">
                      <CarouselView x:Name="CarouselMessages" PositionChanged="OnPositionChanged">
                        <CarouselView.ItemTemplate>
                          <DataTemplate>
                            <StackLayout HorizontalOptions="Fill" BackgroundColor="#f5f5f5">
                              <StackLayout VerticalOptions="Start" Padding="10,10,10,5">
                                <Label Text="{Binding Sender}" FontAttributes="Bold" />
                              </StackLayout>
                              <StackLayout VerticalOptions="FillAndExpand" Padding="10,10,10,10" BackgroundColor="White">
                                <ScrollView>
                                    <Label Text="{Binding Body}" />
                                </ScrollView>
                              </StackLayout>
                              <StackLayout x:Name="attachmentsLayout" BindableLayout.ItemsSource="{Binding Attachments}" VerticalOptions="End" Padding="10,10,10,10" BackgroundColor="White">
                                <BindableLayout.ItemTemplate>
                                    <DataTemplate>
                                        <Label>
                                            <Label.FormattedText>
                                                <FormattedString>
                                                    <local:HyperlinkSpan Text="{Binding Filename}" Url="{Binding Url}" />
                                                </FormattedString>
                                            </Label.FormattedText>
                                        </Label>
                                    </DataTemplate>
                                 </BindableLayout.ItemTemplate>
                              </StackLayout>
                              <StackLayout VerticalOptions="End" Padding="10,5,10,10">
                                <Label Text="{Binding Created_At}" BackgroundColor="#f5f5f5" />
                              </StackLayout>
                            </StackLayout>
                          </DataTemplate>
                        </CarouselView.ItemTemplate>
                      </CarouselView>
                    </ContentView>
                </StackLayout>
                <StackLayout x:Name="editorLayout" VerticalOptions="FillAndExpand" IsVisible="false" Padding="10,0,10,0">
                    <Frame x:Name="replyFrame" HeightRequest="{Binding Path=Height, Source={x:Reference editorLayout}}" HasShadow="false" OutlineColor="Gray" Padding="2,0,2,0">
                    <Editor x:Name="replyEditor">
                        <Editor.BackgroundColor>
                            <OnPlatform x:TypeArguments="Color"
                                iOS="White"
                                Android="Transparent"
                                WinPhone="#2c3e50" />
                        </Editor.BackgroundColor>
                    </Editor>
                    </Frame>
                </StackLayout>
                <StackLayout VerticalOptions="End" Padding="10,3,10,10">
                    <Button x:Name="replyButton" Text="Reply" Clicked="OnReplyActivated" BorderRadius="5" BackgroundColor="#337ab7" TextColor="White" FontAttributes="Bold" />
                    <Button x:Name="sendButton" Text="Send Message" Clicked="OnSendMessageActivated" BorderRadius="5" BackgroundColor="#337ab7" TextColor="White" FontAttributes="Bold" IsVisible="false"/>
                </StackLayout>
            </StackLayout>
            <ContentView x:Name="overlay" AbsoluteLayout.LayoutBounds="0, 0, 1, 1"  AbsoluteLayout.LayoutFlags="All" IsVisible="True" BackgroundColor="#C0808080" Padding="10, 0">
                <ActivityIndicator  WidthRequest="110" HeightRequest="70" IsRunning="True" IsVisible="True" Color="Black" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
            </ContentView>
        </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>
xamarin.forms datatrigger ibindabletemplate
1个回答
0
投票

为什么不简单地向ViewModel添加另一个属性,以指示Attachments集合中是否有任何项目。然后将布局的IsVisible绑定到此属性

查看

<StackLayout x:Name="attachmentsLayout" 
             IsVisible="{Binding HasItems}"
             BindableLayout.ItemsSource="{Binding Attachments}"
             VerticalOptions="End" Padding="10,10,10,10" BackgroundColor="White">
<BindableLayout.ItemTemplate>
    <DataTemplate>
        <Label>
            <Label.FormattedText>
                <FormattedString>
                    <local:HyperlinkSpan Text="{Binding Filename}" Url="{Binding Url}" />
                </FormattedString>
            </Label.FormattedText>
        </Label>
    </DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>

ViewModel

公共布尔HasItems => Attachments.Any();

© www.soinside.com 2019 - 2024. All rights reserved.