代码之家  ›  专栏  ›  技术社区  ›  RKh

如何隐藏网格行和ListView列,以使空白区域不可见?

  •  0
  • RKh  · 技术社区  · 7 年前

    我对Xamarin Grid和ListView有两个问题。

    (1)在ListView中,我有七列。根据条件,第五列和第六列需要以这样的方式隐藏,即第四列之后没有可见的空白。我试图设置isvisble=false,但它在中间显示空白。

    (2)电网也存在类似问题。在ContentView中,我有10行网格。基于某些条件,我想隐藏第七行和第八行,这样空部分就会折叠。用户不能查看空行。

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

    GridView gv = listview.View as GridView;
    GridViewColumn cd = gv.Columns[0];
    gv.Columns.Remove(cd);
    gv.Columns.Add(cd);
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Depechie    7 年前

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

    您可以将行高设置为 Auto 然后把 IsVisible 要隐藏的内容的属性。

    推荐文章