XAML 堆栈布局的.NET MAUI IsVisible多触发器不工作

4smxwvx5  于 2022-12-16  发布在  .NET
关注(0)|答案(2)|浏览(197)

如果我的Picker的SelectedIndex位于最后三个选项上而不是前两个选项上,我尝试将StackLayout Visibility设置为False。
以下是选取器的列表:

"1 - Yes",
"2 - No",
"97 - Don't Know",
"98 - No Answer",
"99 - Not Applicable"

XAML:

<!-- Intention to Use Family Planning --> 
<StackLayout Padding="10,0,10,10"> 
    <Label Text="Intention to use Family Planning?" 
           VerticalTextAlignment="Center" 
           FontAttributes="Bold"/> 
    <Border Padding="10,0,0,0"> 
        <StackLayout> 
            <Picker x:Name="PckIntentionFP" 
                    Title="Select a Family Planning Intention Option" 
                    ItemsSource="{Binding IntentionToFamilyPlanningItems}">
            </Picker>
        </StackLayout> 
    </Border> 
</StackLayout> 

<!-- Intention Yes/No -->
<StackLayout Padding="10,0,10,10">
    <StackLayout.Triggers>
        <MultiTrigger TargetType="StackLayout">
            <MultiTrigger.Conditions>
                <BindingCondition Binding="{Binding Source={x:Reference PckIntentionFP}, 
                                                    Path=SelectedIndex}" 
                                  Value="2"/>
                <BindingCondition Binding="{Binding Source={x:Reference PckIntentionFP}, 
                                                    Path=SelectedIndex}" 
                                  Value="3"/>
                <BindingCondition Binding="{Binding Source={x:Reference PckIntentionFP}, 
                                                    Path=SelectedIndex}"
                                  Value="4"/>
            </MultiTrigger.Conditions>
            <Setter Property="IsVisible" Value="False" />
        </MultiTrigger>
    </StackLayout.Triggers>
    <Label Text="If 'Yes', what method? If 'No', why not?"
           FontAttributes="Bold"
           VerticalTextAlignment="Center"/>
    <Frame Padding="10,0,10,0"
           Margin="5">
        <Entry VerticalOptions="Center"
               TextTransform="Uppercase"
               Text="{Binding LastName}">
        </Entry>
    </Frame>
</StackLayout>

这是我第一次在这里问问题,事先抱歉。
当我在平板电脑中运行应用程序时,当我选择最后三个选项时,它不会将StackLayout Visibility设置为False。
我真的很困惑,我可能错过了什么使用多触发器。

fgw7neuy

fgw7neuy1#

您可以使用Picker的SelectedIndexChanged处理程序。
在xaml文件中:

<Picker x:Name="PckIntentionFP" 
    Title="Select a Family Planning Intention Option" 
    ItemsSource="{Binding IntentionToFamilyPlanningItems}"
    SelectedIndexChanged="PckIntentionFP_SelectedIndexChanged"> // add this event handler for picker

//set a name for StackLayout
<StackLayout Padding="10,0,10,10" x:Name="mystack" >
            ...
               
</StackLayout>

字符串
并在.cs文件中实现它:

void PckIntentionFP_SelectedIndexChanged(System.Object sender, System.EventArgs e)
{
    var p = sender as Picker;
    if (p.SelectedIndex == 1 || p.SelectedIndex == 0)
    {
        mystack.IsVisible = true;
    }else
    {
        mystack.IsVisible = false;
    }
}

另一种方法我认为您可以分别为所选索引2、3和4使用三个并行数据触发器。

希望对你有用。

8fq7wneg

8fq7wneg2#

你想要布尔值。(IsVisible)。你有整数。(选定的索引)。
您可以使用值转换器进行绑定。
https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/data-binding/converters?view=net-maui-7.0
第一个例子是int到bool的转换。如果你有问题,请问。

相关问题