Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set TextColor Xamarin.Forms TableSection?

I am a fan of doing as much as possible in xaml, I have aTableView` with a TableSection.

<TableView Intent="Menu">
     <TableRoot>
          <TableSection Title="Test Section" TextColor="#FFFFFF">
                <TextCell Text="Test Item" TextColor="#FFFFFF"/>
          </TableSection>
     </TableRoot>
</TableView>

For TextCell TextColor="#FFFFFF" seems to work, however whenever I use this attribute on a TableSection I get this:

An unhandled exception occurred.

Is it possible to change the color of the TableSection with xaml?

like image 770
Trevi Awater Avatar asked Sep 26 '22 22:09

Trevi Awater


1 Answers

Custom Renderers! I have two blog posts on this here:

Android:Xamarin.Forms TableView Section Custom Header on Android

iOS: Xamarin.Forms TableView Section Custom Header on iOS

Basically, create a custom view that inherits TableView, then custom renderers that implement custom TableViewModelRenderer. From there you can override methods to get the header view for the section title.

Here's what that might look like for Android:

public class ColoredTableViewRenderer : TableViewRenderer
{

    protected override TableViewModelRenderer GetModelRenderer(Android.Widget.ListView listView, TableView view)
    {
        return new CustomHeaderTableViewModelRenderer(Context, listView, view);
    }

    private class CustomHeaderTableViewModelRenderer : TableViewModelRenderer
    {
        private readonly ColoredTableView _coloredTableView;

        public CustomHeaderTableViewModelRenderer(Context context, Android.Widget.ListView listView, TableView view) : base(context, listView, view)
        {
            _coloredTableView = view as ColoredTableView;
        }

        public override Android.Views.View GetView(int position, Android.Views.View convertView, ViewGroup parent)
        {
            var view = base.GetView(position, convertView, parent);

            var element = GetCellForPosition(position);

            // section header will be a TextCell
            if (element.GetType() == typeof(TextCell))
            {
                try
                {
                    // Get the textView of the actual layout
                    var textView = ((((view as LinearLayout).GetChildAt(0) as LinearLayout).GetChildAt(1) as LinearLayout).GetChildAt(0) as TextView);

                    // get the divider below the header
                    var divider = (view as LinearLayout).GetChildAt(1);

                    // Set the color
                    textView.SetTextColor(_coloredTableView.GroupHeaderColor.ToAndroid());
                    textView.TextAlignment = Android.Views.TextAlignment.Center;
                    textView.Gravity = GravityFlags.CenterHorizontal;
                    divider.SetBackgroundColor(_coloredTableView.GroupHeaderColor.ToAndroid());
                }
                catch (Exception) { }
            }

            return view;
        }
    }
}

And the on iOS:

public class ColoredTableViewRenderer : TableViewRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<TableView> e)
    {
        base.OnElementChanged(e);
        if (Control == null)
            return;

        var coloredTableView = Element as ColoredTableView;
        tableView.WeakDelegate = new CustomHeaderTableModelRenderer(coloredTableView);
    }



    private class CustomHeaderTableModelRenderer : UnEvenTableViewModelRenderer
    {
        private readonly ColoredTableView _coloredTableView;
        public CustomHeaderTableModelRenderer(TableView model) : base(model)
        {
            _coloredTableView = model as ColoredTableView;
        }
        public override UIView GetViewForHeader(UITableView tableView, nint section)
        {
            return new UILabel()
            {
                Text = TitleForHeader(tableView, section),
                TextColor = _coloredTableView.GroupHeaderColor.ToUIColor(),
                TextAlignment = UITextAlignment.Center
            };
        }
    }
}
like image 73
SuavePirate Avatar answered Sep 30 '22 07:09

SuavePirate