Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

xamarin.forms - Listview not updating custom dataTemplate xamarin Groupheadertemplate

I have a grouped list view which I can't seem to be able to customize without getting a system exception. I just want to customize my grouping header, but I ma not doing something right.

    items = await _manager.GetItems();
                 var sorted = from item in items
                              orderby item.TypeSort
                              group item by item.TypeSort into itemGroup
                              select new ItemTypeGroup <string, Item>(itemGroup.Key, itemGroup);
    groups = new ObservableCollection<ItemTypeGroup<string, Item>>(sorted);
      ItemList.IsGroupingEnabled = true;
                    ItemList.ItemsSource = groups;
                    ItemList.GroupDisplayBinding = new Binding("GroupKey");
     ItemList.GroupHeaderTemplate = new DataTemplate(typeof(CustomViewCell));

    XAML

  <ListView  x:Name="ItemList."
                       HorizontalOptions="Center"
                       ItemSelected="OpenDetails"
                       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 Name}"
                                            Grid.Row="0"
                                            Grid.Column="0"
                                            TextColor="#5e5e5e"
                                            Padding="5"
                                            Margin="15,5,5,5"
                                            LineBreakMode="WordWrap">
                                    </Label>
                                    <Label Text="{Binding Note}"
                                           Grid.Row="1"
                                           Grid.Column="0"
                                           Margin="15,5,5,5"
                                           TextColor="#5e5e5e">
                                    </Label>
                                </Grid>
                               <!-- -->
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
             </ListView>



    **CustomViewCell**

     public class CustomViewCell: ViewCell
        {
            public CustomViewCell()
            {
                this.Height = 35;
                var title = new Label
                {
                    TextColor = Color.Black,
                    BackgroundColor = Color.FromHex("#eeeff2"),
                    FontSize = 18,
                    FontAttributes = FontAttributes.Bold,
                    VerticalOptions = LayoutOptions.Center
                    
                };
    
                title.SetBinding(Label.TextProperty, "Key");
    
                View = new StackLayout
                {
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    HeightRequest = 25,
                    BackgroundColor = Color.FromHex("#eeeff2"),
                    Padding = 5,
                    Orientation = StackOrientation.Horizontal,
                    Children = { title }
                };
            }
        }

When I try to add an GroupedHeaderTemplate, nothing changes. Do I have to do a custom renderer instead? Or Am I doing something wrong?

question from:https://stackoverflow.com/questions/65866676/listview-not-updating-custom-datatemplate-xamarin-groupheadertemplate

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...