如何在Xamarin表单中更新listview中单击行的rowheight?

时间:2018-04-03 02:18:23

标签: xamarin xamarin.forms

这是我当前的listview XAML,它可以正常填充我的listview数据。我尝试通过使stacklayout中的项目可见/不可见来调整独特单元格的行高度,同时通过调整stacklayout和grid的heightrequest来调整它的行高。

    <ContentPage.Resources>
    <ResourceDictionary>
        <DataTemplate
            x:Key="ClassTemplate">
            <ViewCell> <!-- I've tried to bind viewchells height (Height = "{Binding RowHeight}") but its not allowed to bind it -->

                <StackLayout HeightRequest = "{Binding RowHeight}" HorizontalOptions = "FillAndExpand" VerticalOptions = "FillAndExpand">

                <Button   HeightRequest = "10" />
                <Button  IsVisible = "{Binding ExpandedRowVisibility}" HeightRequest = "300" />

                <Grid VerticalOptions="Fill" HeightRequest = "{Binding RowHeight}">
                    <Grid.GestureRecognizers>
                        <TapGestureRecognizer
                            Command="{Binding OpenRowDetails}"
                            CommandParameter="{Binding .}"
                            NumberOfTapsRequired="1" />
                    </Grid.GestureRecognizers>
                </Grid>

                </StackLayout>

            </ViewCell>
        </DataTemplate>
    </ResourceDictionary>
</ContentPage.Resources>


<ListView
ItemsSource="{Binding List}"
HasUnevenRows="true"
Header="{Binding ShowHeaderObject}"
ItemTemplate="{StaticResource ClassTemplate}">

<ListView.HeaderTemplate>
   <DataTemplate>
      <StackLayout>
          <Label
          Text="{Binding .}"
          HorizontalTextAlignment="Center"
          TextColor="White" />
       </StackLayout>
     </DataTemplate>
  </ListView.HeaderTemplate>

  </ListView>

当我单击列表中的一行时,我更新了绑定到Stacklayout,网格和按钮的RowHeight值(我已经尝试了所有三个,单独和一起)但是我点击的行不会更新。

    private string Height = "140";
    public string RowHeight
    {
        get { return Height; }
        set
        {

    private bool ExpandRow;

    public bool ExpandedRowVisibility
    {
        get { return ExpandRow; }
        set
        {
            this.ExpandRow = value;
            OnPropertyChanged("ExpandedRowVisibility");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged([CallerMemberName]string propertyName = null)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

public ICommand OpenRowDetails
    {
        get
        {
            return new Command(e =>
            {

                var Item = (ClassItemViewModel)e;
                Item.RowHeight = "280";
                Item.ExpandedRowVisibility = true;
            });
        }
    }

如何更新我的代码,以便点击的行会调整其行高,但其他行保持不变?

1 个答案:

答案 0 :(得分:0)

试试这个

 <ListView x:Name="listView" HasUnevenRows="true">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout HorizontalOptions = "FillAndExpand"  VerticalOptions = "FillAndExpand">
                        <StackLayout.GestureRecognizers>
                            <TapGestureRecognizer Tapped="OnItemTapped" NumberOfTapsRequired="1">
                           </StackLayout.GestureRecognizers>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

在您的点按事件方法中,更改布局的高度并更新单元格

void OnItemTapped(object sender, EventArgs args)
{
    var layout = sender as StackLayout;
    var viewCell = layout.Parent as ViewCell;
    layout.HeightRequest = layout.Height + 100;
    viewCell.ForceUpdateSize ();
}