xamarin/Xamarin.Forms

Grid height ignore change after it was set to 0 if it is in the listview.

开放

#4,589 创建于 2018年11月30日

 (1 条评论) (0 个反应) (0 位负责人)C# (1,926 个派生)batch import
a/grida/layouta/listviewe/6 :clock6:help wantedi/lowinactivet/bug :bug:up-for-grabs

仓库指标

星标
 (5,644 个星标)
PR 合并指标
 (30 天内没有已合并 PR)

描述

Description

I have a gird inside a listview. If I set the gird height to 0, then it will not show/change any more when I set the height to other value. It only happens when the gird inside a listview.

Steps to Reproduce

  1. Create a listview and grid.
    <StackLayout>
        <Button x:Name="BtnHide" Clicked="BtnHide_Clicked" Text="Hide" />
        <Button x:Name="BtnShow" Clicked="BtnShow_Clicked" Text="Show" />
        <ListView ItemsSource="{Binding BindableThings}" HasUnevenRows="True">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="{Binding BoundHeight}" />
                            </Grid.RowDefinitions>
                            <Label Grid.Row="0"  Text="{Binding TestString}" />
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
  1. Set the BindingContext and set button click event. In the click event change the grid height.
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            this.BindingContext = this;
        }

        public List<BindableThing> BindableThings { get; set; } = new List<BindableThing>()
        {
            new BindableThing() { TestString = new String ('*', 200)  },
            new BindableThing() { TestString = new String ('!', 200)  },
        };

        private void BtnHide_Clicked(object sender, EventArgs e)
        {
            BindableThings[0].BoundHeight = new GridLength(0);
            BindableThings[1].BoundHeight = new GridLength(1);
        }

        private void BtnShow_Clicked(object sender, EventArgs e)
        {
            BindableThings[0].BoundHeight = new GridLength(50);
            BindableThings[1].BoundHeight = new GridLength(50);
        }
    }

    public class BindableBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void propchg(string propname)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propname));
        }
    }

    public class BindableThing : BindableBase
    {
        public string TestString { set; get; }
        GridLength _BoundHeight = new GridLength(50);
        public GridLength BoundHeight {
            get => _BoundHeight;
            set {
                _BoundHeight = value;
                propchg(nameof(BoundHeight));
            }
        }
    }

Expected Behavior

Click BtnHide the grid hide and click BtnShow the grid show.

Actual Behavior

Click BtnHide the grid hide and click BtnShow the first grid which height is set to 0 not show.

Basic Information

  • Version with issue: Xamarin.Forms 3.3.0.967583
  • Last known good version:
  • IDE: Visual Studio 2017 enterprise 15.9.3
  • Platform Target Frameworks:
    • Android: 8.1

贡献者指南