【问题标题】:How to update the rowheight of a clicked row in a listview in Xamarin forms?如何更新 Xamarin 表单中列表视图中单击行的行高?
【发布时间】:2018-04-03 02:18:23
【问题描述】:

这是我当前有效的列表视图 XAML,我成功地用数据填充了我的列表视图。我尝试通过使堆栈布局中的项目可见/不可见以及通过调整堆栈布局和网格的高度请求来调整唯一单元格的行高。

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

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

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    试试这个

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

    【讨论】:

    • var viewCell = layout.Parent as ViewCell; Object reference not set to the instance of an objectcrash
    • 对不起,我现在不在我的机器上,你能调试并检查一下你在这里得到的父元素到底是什么吗?
    • 我该怎么做?当我这样做 var send = e as Stacklayout; System.Diagnostics.Debug.WriteLine(send.Parent); 时,我会遇到同样的崩溃
    • 是的,试图将其更改为网格和 Button 以及 Taprecognizer,但同样的崩溃
    • 你在sender对象中得到了什么元素?可以为此添加调试吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多