如何隐藏Grid行和ListView列,以便看不到空白空间? [英] How to hide a Grid row and ListView column so that no empty space is visible?

查看:229
本文介绍了如何隐藏Grid行和ListView列,以便看不到空白空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Xamarin Grid和ListView中遇到两个问题.

I have two issues with Xamarin Grid and ListView.

(1)在ListView中,我有七列.根据条件,需要隐藏第五和第六列,以使第四列之后没有空白可见.我尝试设置IsVisble = false,但它之间显示了空白.

(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)网格也有类似问题.在ContentView内部,我有十行网格.根据特定条件,我要隐藏第7行和第8行,以使空白部分可以折叠.用户应该无法查看空白行.

(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.

如果我从下面的代码中尝试使用以下代码删除行,则我怀疑.XAML可能会崩溃,因为需要对行号进行重新排序.

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);

推荐答案

对于网格问题,只需确保使用Binding来动态设置RowHeight. 因此,只要您想隐藏这些行,就可以将高度设置为0.

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.

代码如下:

<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;
    }
}

这篇关于如何隐藏Grid行和ListView列,以便看不到空白空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆