Xamarin Forms - How to Make iOS ListView Native Grouped Style With Headers?

我怕爱的太早我们不能终老 提交于 2019-12-10 18:45:55

问题


I have created a listview in Xamarian Forms using MVVM and binding to create a grouped listview.I would like the listview to have the UITableView Style group which is for iOS. I tried creating this via a custom renderer which I found online, but the result looks wrong as everything is gray.

First we will start with the images. This is what I want the table to look like whcih I took from the native iOS version:

When I add the custom renderer I get the following for the Xamarin forms version:

I played around with background colors trying to make them transparent, and I found that when I made the listview background color = "Transparent" that the original listview was under. Shown in the following two images.

The xaml page is as follows:

<StackLayout Spacing="0">
    <SearchBar x:Name="SearchBarControl" Text="{Binding SearchText}" SearchCommand="{Binding SearchCommand}" Placeholder="Search"></SearchBar>
    <ListView x:Name="HistoryList" IsGroupingEnabled="True" GroupDisplayBinding="{Binding Title}" ItemsSource="{Binding History}" CachingStrategy="RecycleElement">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextCell Text="{Binding TagContent}" Detail="{Binding DateAndTime}" />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackLayout>

The custom renderer that I believe should make it grouped:

[assembly: ExportRenderer(typeof(ListView), typeof(iOSListViewCustomRenderer))]
namespace Test.iOS.CustomRenderer
{
    public class iOSListViewCustomRenderer : ListViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
        {
            base.OnElementChanged(e);
            if (this.Control != null && e.NewElement != null)
            {
                var tbl = new UITableView(this.Bounds, UITableViewStyle.Grouped)
                {
                    Source = this.Control.Source,
                };

                this.SetNativeControl(tbl);
            }
        }
    }
}

If anyone needs to see anything else, let me know.

I know the tableview control in forms gives me the proper look but it seems to be static content and not dynamic info from a SQL database.

I tried making the cells white by making a custom cell but that didn't change anything.

How can I get the xamarin forms list view to look like the first image with the cells being white and be in that styled group?


回答1:


I suggest you use GroupHeaderTemplate instead of Custom Renderers.

You can custom Group Header of listView instead of using the normal style.

<ListView.GroupHeaderTemplate>
    <DataTemplate>
        <ViewCell Height="50" >
            <ViewCell.View>
                <StackLayout >
                    <Label Text ="{Binding Title}" Margin="15,30,0,5" FontSize="Medium" TextColor="Gray" />
                </StackLayout>
            </ViewCell.View>
        </ViewCell>
    </DataTemplate>
</ListView.GroupHeaderTemplate>

GroupDisplayBinding is no needed , and set HasUnevenRows to enable increasing the cell height.

<ListView x:Name="listView" IsGroupingEnabled="True" HasUnevenRows="True">

Detail refer to Here



来源:https://stackoverflow.com/questions/47851691/xamarin-forms-how-to-make-ios-listview-native-grouped-style-with-headers

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