Home > OS >  How to set default value to false when using converter with Label's visibility Xamarin forms
How to set default value to false when using converter with Label's visibility Xamarin forms

Time:09-22

I am using converter with Label's IsVisible property.

<Label IsVisible="{Binding products, Converter={StaticResource EmptyCollectionToBoolConverter}}" Text="No data found">  

If products is empty EmptyCollectionToBoolConverter returns true otherwise false. When screen is loading first time "No data found" message appears for fraction of seconds and then data is getting loaded.

I want to fix it, I need to show Label only if when products is empty. How can I do it?

CodePudding user response:

If you are using a CollectionView you can use the EmptyView , it will display whatever you put in that XAML when the collection is empty.

Or you can implement bindablelayout that also implements the emptyViewTemplate.

Or you will have to create another binding or another converter.

CodePudding user response:

You can overwrite IsVisible value in the code behind.

<Label x:Name="MyLabel" IsVisible="{Binding products, Converter={StaticResource EmptyCollectionToBoolConverter}}" Text="No data found">

Code behind

// probably ctor
MyLabel.IsVisible = false;

Second option can be to use a DataTrigger

<Label Text="No data found" IsVisible="false">
    <Label.Triggers>
        <DataTrigger TargetType="Label" Binding="{Binding products, Converter={StaticResource EmptyCollectionToBoolConverter}}" Value="True">
            <Setter Property="IsVisible" Value="True" />
        </DataTrigger>
    </Label.Triggers>
</Label>
  • Related