Home > Net >  If Condition Maui Xaml Without Triggers
If Condition Maui Xaml Without Triggers

Time:10-02

Is it possible to implement an if condition statement in dotnet maui on xaml page without Triggers .

At least in my experience I find them tedious or not very simple to implement in case (for example) i need to check if a string lenght it's between 5 or 10 characters. I know that xamarin forms lead with the same issue. But doesn't find any simple way to make this. Is there any library or something that i missed out that simplify this problem.

I know that blazor work's different but would like to implement if contidion like this:

<VerticalStackLayout>
    @if (item.lenght > 5)
    {
        <Label  Text="Lenght should be higher than 5" />
    }
</VerticalStackLayout>

All info / Code samples / Enhancement / Discussions / Recommendations, they are welcome, I see that it is a very normal topic and of which there are not so many examples

CodePudding user response:

I think you can set an IsVisible property on Label with binding a parameter like this:

<Label IsVisible="{Binding LabelLength}" Text="Lenght should be higher than 5"></Label>

Then you create a method in your viewmodel to realize the LabelLength.

 public bool LabelLength(String item)
        {
            return item != null && item.Length >= 5;
        }

This can check the item whether the length is bigger than 5.

CodePudding user response:

You could bind the text to the Visibility property of the Label and then use a converter (s. https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/data-binding/converters), to convert the text to a boolean value indicating, that the text is too short.

  • Related