Auto size ListView height when ListView item gets modified

亡梦爱人 提交于 2021-01-29 09:27:52

问题


I have been having some issues resizing my list view row to match the text inside of it. Also I have read a lot of documentation and everything seems to point toward using HasUnevenRows, which I am using but isn't quite what I need.

I have a ListView which I allow the user to modify contents of this row using Rg.Popups and context actions. Essentially the user swipes and clicks Edit and they get pushed to a popup where they can edit the list view item.

When the note gets updated it also gets updated on my ListView however if the note is more than 1 line the row will not resize. I have to leave the page and come back to it for the row to resize to fit the entire note.

Below is my code.

  <ListView  x:Name="MyItemList"
                       HorizontalOptions="Center"
                       ItemSelected="OpenRecipeDetails"
                       IsGroupingEnabled="True"
                       HasUnevenRows="true">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell x:Name="myViewcell">
                            <ViewCell.ContextActions>
                                <MenuItem Clicked="MenuItem_Clicked" Text="Edit Note" CommandParameter="{Binding .}"/>
                            </ViewCell.ContextActions>
                            <StackLayout>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="Auto" />
                                    </Grid.RowDefinitions>
                                    <Label Text="{Binding Title}"
                                            Grid.Row="0"
                                            Grid.Column="0"
                                            FontSize="{Binding BindingContext.H1Font, Source={x:Reference Name=ThisPage}}"
                                            Padding="5"
                                            
                                            LineBreakMode="WordWrap">
                                    </Label>
                                    <Label Text="{Binding sNote}"
                                           Grid.Row="1"
                                           Grid.Column="0"  
                                           FontSize="{Binding BindingContext.H2Font, Source={x:Reference Name=ThisPage}}"
                                          >
                                    </Label>
                                </Grid>
                               <!-- -->
                            </StackLayout>
                        </ViewCell>

                    </DataTemplate>
                </ListView.ItemTemplate>
             </ListView>

CodeBehind

//for editing the note
 async void MenuItem_Clicked(System.Object sender, System.EventArgs e)
        {
            var menuItem = sender as MenuItem;
            updatedItem = menuItem.CommandParameter as MyItemBooklet;
            await PopupNavigation.Instance.PushAsync(new EditNotesPopupPage(updatedItem));
        }

EditNotesPopupPage

 public EditNotesPopupPage( MyItemBooklet item)
        {
            InitializeComponent();
            _item = item;
        }
...
  async void Cancel_Clicked(System.Object sender, System.EventArgs e)
        {
            await PopupNavigation.Instance.PopAllAsync();
        }

       async void Ok_Clicked(System.Object sender, System.EventArgs e)
        {
           ...
           ...

            await db.UpdateNote(text, _item.iRecipeLinkID);
            await PopupNavigation.Instance.PopAllAsync();
        }

回答1:


You can try force update size of ViewCell below is the link with good sample that will guide you:

https://docs.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/userinterface-listview-dynamicunevenlistcells/



来源:https://stackoverflow.com/questions/65838416/auto-size-listview-height-when-listview-item-gets-modified

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!