5

I have two issues with Xamarin Grid and ListView.

(1) In ListView, I have seven columns. Based on a condition, the fifth and sixth columns need to be hidden in such a way that there is no blank space visible after forth column. I tried to set IsVisble=false but it shows blank space in between.

(2) Similar issue is with Grid. Inside a ContentView, I have Grid with ten rows. Based on certain condition, I want to hide rows seven and eight in such a way that the empty portion should get collapsed. User should not be able to view the empty row.

If from code-behind I try removing rows using the below code, I suspect the .XAML may crash as row numbers need to be reordered.

GridView gv = listview.View as GridView;
GridViewColumn cd = gv.Columns[0];
gv.Columns.Remove(cd);
gv.Columns.Add(cd);

2 Answers 2

9

For the grid problem, just be sure to use Binding to set the RowHeight dynamically. So as soon as you want to hide those rows, you set the height to 0.

Code would look like this:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Test"
             x:Class="Test.MainPage">
    <StackLayout Margin="0,20,0,0">
        <Grid RowSpacing="0" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
            <Grid.RowDefinitions>
                <RowDefinition Height="50" />
                <RowDefinition Height="{Binding RowHeight}" />
                <RowDefinition Height="{Binding RowHeight}" />
                <RowDefinition Height="50" />
                <RowDefinition Height="50" />
            </Grid.RowDefinitions>

            <Label Text="Row 1" Grid.Row="0" HorizontalTextAlignment="Center" />
            <Label Text="Row 2" Grid.Row="1" HorizontalTextAlignment="Center" />
            <Label Text="Row 3" Grid.Row="2" HorizontalTextAlignment="Center" />
            <Label Text="Row 4" Grid.Row="3" HorizontalTextAlignment="Center" />
            <Label Text="Row 5" Grid.Row="4" HorizontalTextAlignment="Center" />
        </Grid>

        <Button Text="Hide rows" Clicked="OnClicked" />
    </StackLayout>
</ContentPage>

public partial class MainPage : ContentPage, INotifyPropertyChanged
{
    private int _rowHeight = 50;
    public int RowHeight
    {
        get => _rowHeight;
        set
        {
            _rowHeight = value;
            OnPropertyChanged();
        }
    }

    public MainPage()
    {
        InitializeComponent();
        BindingContext = this;
    }

    private void OnClicked(object sender, System.EventArgs e)
    {
        RowHeight = 0;
    }
}
5
  • 1
    Thanks a lot. I will try. I guess this trick should work for columns as well if column width is reduced to zero.
    – RKh
    Commented Jun 17, 2018 at 16:21
  • How to animate it though? just making it invisible suddenly is not elegant
    – Ali123
    Commented Sep 20, 2021 at 10:27
  • I have not tried this myself... but seems it should be doable? johankarlsson.net/2015/02/animate-height-of-rowdefinition.html
    – Depechie
    Commented Sep 22, 2021 at 7:35
  • It threw me for a loop that you made RowHeight an int. How do you set that to "*"? At any rate it works for integers. Now I can set RowHeight = 0 and the space it was using disappears and is reallocated to the other elements. I am using Maui BTW. Thanks! Commented Nov 15, 2022 at 20:35
  • @charlesyoung maybe check Gerald his solution? blog.verslu.is/xamarin/xamarin-forms-xamarin/…
    – Depechie
    Commented Nov 17, 2022 at 14:10
8

You can set the row Height to Auto and then bind the IsVisible property of the content you want to hide.

2
  • Thank you @Daniel, this work like a charm and is the best solution in my opinion Commented Jul 29, 2020 at 14:02
  • 4
    Yes, but doesnt work with rowspace>0, then you get ugly blank spaces where hidden rows should have been. IMHO this is a xamarin forms bug...
    – Tompi
    Commented Sep 15, 2021 at 12:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.