MPowerKit / VirtualizeListView

.NET MAUI ListView renderers. Improves performance for MAUI ListView, adds additional behaviors
MIT License
69 stars 10 forks source link

EmptyView #4

Closed andersonvieiragomeslopes closed 1 month ago

andersonvieiragomeslopes commented 2 months ago

I'm using the component and it looks really good, the scrolling is incredibly fluid. Do you intend to add EmptyView to the component?

Alex-Dobrynin commented 1 month ago

This will not be implemented in this library, but you can easily achieve this using DataTrigger or Converter or CompiledBindings library.

DataTrigger:

<Grid>
    <YourEmptyView IsVisible="False">
        <YourEmptyView.Triggers>
            <DataTrigger Binding="{Binding Items.Count}"
                         TargetType="YourEmptyView"
                         Value="0">
                <Setter Property="IsVisible"
                        Value="True" />
            </DataTrigger>
        </YourEmptyView.Triggers>
    </YourEmptyView>

    <mpk:FixedRefreshView Command="{Binding RefreshCommand}"
                          IsRefreshing="{Binding RefreshCommand.IsRunning, Mode=OneWay}">
        <mpk:VirtualizeListView>
            <mpk:VirtualizeListView.Triggers>
                <DataTrigger Binding="{Binding Items.Count}"
                             TargetType="mpk:VirtualizeListView"
                             Value="0">
                    <Setter Property="IsVisible"
                            Value="False" />
                </DataTrigger>
            </mpk:VirtualizeListView.Triggers>
        </mpk:VirtualizeListView>
    </mpk:FixedRefreshView>
</Grid>

Converter

<Grid>
    <YourEmptyView IsVisible="{Binding Items.Count, Converter={StaticRecource IntToBooleanInvertedConverter}}" />

    <mpk:FixedRefreshView Command="{Binding RefreshCommand}"
                          IsRefreshing="{Binding RefreshCommand.IsRunning, Mode=OneWay}">
        <mpk:VirtualizeListView IsVisible="{Binding Items.Count, Converter={StaticRecource IntToBooleanConverter}}" />
    </mpk:FixedRefreshView>
</Grid>

in your App.xaml

<Application x:Class="MauiApp1.App"
             xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:converters="clr-namespace:MauiApp1.Converters">
    <Application.Resources>
        <ResourceDictionary>
            <!-- your other styles -->

            <converters:IntToBooleanConverter x:Key="IntToBooleanConverter" />
            <converters:IntToBooleanConverter x:Key="IntToBooleanInvertedConverter"
                                              IsInverted="True" />
        </ResourceDictionary>
    </Application.Resources>
</Application>

your converter

namespace MauiApp1.Converters
{
    /// <summary>
    /// Converts the value of type integer to a bool depending on the result of the specified expression.
    /// If the value is equal to threshold, it returns false. If this value is not equal to threshold, it returns true.
    /// And vice versa if converter is inverted.
    /// </summary>
    public class IntToBooleanConverter : IValueConverter
    {
        public bool IsInverted { get; set; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int threshold = 0;
            if (parameter is string str)
            {
                int.TryParse(str, out threshold);
            }
            else if (parameter is int intParameter)
            {
                threshold = intParameter;
            }

            if (value is int val)
            {
                return IsInverted ? val <= threshold : val > threshold;
            }

            return IsInverted;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

CompiledBindings library

<Grid>
    <YourEmptyView IsVisible="{x:Bind Items.Count eq 0}" />

    <mpk:FixedRefreshView Command="{Binding RefreshCommand}"
                          IsRefreshing="{Binding RefreshCommand.IsRunning, Mode=OneWay}">
        <mpk:VirtualizeListView IsVisible="{x:Bind Items.Count gt 0}" />
    </mpk:FixedRefreshView>
</Grid>