使用触发器在Xamarin表单不起作用的情况下在两个堆栈布局之间进行选择

时间:2019-06-05 05:57:24

标签: xamarin xamarin.forms triggers datatrigger

我在 Listview 中有两个堆栈布局。我想根据情况一次显示每个。此处有一个名为 Complete 的值。如果为False,则应显示实验室结果。如果为真,则应显示“文档列表”。这行不通。如何实现?


<StackLayout x:Name="LabLayout" IsVisible="False" Orientation="Vertical" HorizontalOptions="Start" Spacing="0" Margin="0,-6,0,0">
    <StackLayout.Triggers>
        <DataTrigger TargetType="StackLayout" Binding="{Binding Completed}" Value="True">
            <Setter Property="StackLayout.IsVisible" Value="False"/>
        </DataTrigger>
        <DataTrigger TargetType="StackLayout"  Binding="{Binding Completed}" Value="False">
            <Setter Property="StackLayout.IsVisible" Value="True"/>
        </DataTrigger>
    </StackLayout.Triggers>
    <Label Text="{Binding Title}"  TextColor="{StaticResource DarkColor}" FontSize="{StaticResource FontSize17}"/>
    <Label Text="{Binding Author}" TextColor="{StaticResource QuateneryLightColor}" FontSize="{StaticResource FontSize13}"/>
    <StackLayout Orientation="Horizontal">
        <Label Text="{Binding LabItemStatus }" TextColor="{StaticResource QuateneryLightColor}" FontSize="{StaticResource FontSize13}"/>
        <Label Text="{Binding Progress}" TextColor="{StaticResource QuateneryLightColor}" FontSize="{StaticResource FontSize13}"/>
    </StackLayout>
</StackLayout>


<StackLayout x:Name="DocumentLayout" IsVisible="False" Orientation="Vertical" HorizontalOptions="Start" Spacing="0" Margin="0,-6,0,0">
    <StackLayout.Triggers>
        <DataTrigger TargetType="StackLayout" Binding="{Binding Completed}" Value="False">
            <Setter Property="StackLayout.IsVisible" Value="False"/>
        </DataTrigger>
        <DataTrigger TargetType="StackLayout"  Binding="{Binding Completed}" Value="True">
            <Setter Property="StackLayout.IsVisible" Value="True"/>
        </DataTrigger>
    </StackLayout.Triggers>
    <Label Text="{Binding Title}" TextColor="{StaticResource DarkColor}" FontSize="{StaticResource FontSize17}"/>
    <Label Text="{Binding DateTimeString}" TextColor="{StaticResource QuateneryLightColor}" FontSize="{StaticResource FontSize13}"/>
    <StackLayout Orientation="Horizontal">
        <Label Text="{Binding Department }" TextColor="{StaticResource DarkColor}" FontSize="{StaticResource FontSize13}"/>
        <Label Text="||" TextColor="{StaticResource DarkColor}" FontSize="{StaticResource FontSize13}"/>
        <Label Text="{Binding Author}" TextColor="{StaticResource DarkColor}" FontSize="{StaticResource FontSize13}"/>
    </StackLayout>
</StackLayout>

2 个答案:

答案 0 :(得分:0)

对每个布局使用单独的绑定。喜欢

<StackLayout x:Name="LabLayout" IsVisible="{Binding LabLayoutVisibility}" Orientation="Vertical" HorizontalOptions="Start" Spacing="0" Margin="0,-6,0,0">
    <Label Text="{Binding Title}"  TextColor="{StaticResource DarkColor}" FontSize="{StaticResource FontSize17}"/>
    <Label Text="{Binding Author}" TextColor="{StaticResource QuateneryLightColor}" FontSize="{StaticResource FontSize13}"/>
    <StackLayout Orientation="Horizontal">
        <Label Text="{Binding LabItemStatus }" TextColor="{StaticResource QuateneryLightColor}" FontSize="{StaticResource FontSize13}"/>
        <Label Text="{Binding Progress}" TextColor="{StaticResource QuateneryLightColor}" FontSize="{StaticResource FontSize13}"/>
    </StackLayout>
</StackLayout>


<StackLayout x:Name="DocumentLayout" IsVisible="{Binding DocumentLayoutVisibility}" Orientation="Vertical" HorizontalOptions="Start" Spacing="0" Margin="0,-6,0,0">
    <Label Text="{Binding Title}" TextColor="{StaticResource DarkColor}" FontSize="{StaticResource FontSize17}"/>
    <Label Text="{Binding DateTimeString}" TextColor="{StaticResource QuateneryLightColor}" FontSize="{StaticResource FontSize13}"/>
    <StackLayout Orientation="Horizontal">
        <Label Text="{Binding Department }" TextColor="{StaticResource DarkColor}" FontSize="{StaticResource FontSize13}"/>
        <Label Text="||" TextColor="{StaticResource DarkColor}" FontSize="{StaticResource FontSize13}"/>
        <Label Text="{Binding Author}" TextColor="{StaticResource DarkColor}" FontSize="{StaticResource FontSize13}"/>
    </StackLayout>
</StackLayout>

答案 1 :(得分:0)

IValueConverter用于在不同类型之间转换值,在这里您可以创建一个与Completed相反的新变量,并将其绑定到StackLayout.IsVisible

    private bool completed;
    public bool Completed {
        get
        {
            return completed;
        }
        set {
            completed = value;
            UnCompleted= !completed;
            OnPropertyChanged("Completed");
        }
    }


    public bool UnCompleted
    {
        get;
        set;
    }

<StackLayout IsVisible="{Binding UnCompleted}">