对于网格问题,只需确保使用绑定动态设置行高即可。
因此,只要想隐藏这些行,就将高度设置为0。
代码如下:
<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;
}
}