IGridCellConverter - SlowLogicBoy/Eto.Generator GitHub Wiki

By default if GridViewGenerator doesn't know how to convert certain type it uses AnyToGridCellConverter which does object.ToString(). But you can add your own converter:

class DateTimeToGridCellConverter : IGridCellConverter {
    public Cell Convert(PropertyInfo property) {
        if(!typeof(DateTime?).IsAssignableFrom(property.PropertyType)) return null; //We can't convert other types
        return new TextBoxCell{
            Binding = Binding.Delegate((object o) => {
                var dateTime = (property.GetValue(o) as DateTime?);
                if(dateTime.HasValue)
                    return dateTime.Value.ToString("yyyy-MM-dd");
                return "No Date Specified";
            })
        };
    }
}

To use custom converter:

var converters = new IGridCellConverter[] {new DateTimeToGridCellConverter()}.Concat(GridViewGenerator.GetDefaultGridCellConverters());
var gridView = new GridViewGenerator(converters).GetGridView<MyModel>();

Full code:

#! "netcoreapp2.0"

#r "nuget: Eto.Forms, *"
#r "nuget: Eto.Platform.Gtk, *"
#r "Eto.Generator.dll"

using System.Reflection;
using Eto.Forms;
using Eto.Generator;
using Eto.Generator.GridCellConverters;

class MyModel
{
    public string Title { get; set; }
    public bool? IsChecked { get; set; }
    public List<string> StringList { get; set; }
    public DateTime? Time { get; set; }
}

class DateTimeToGridCellConverter : IGridCellConverter {
    public Cell Convert(PropertyInfo property) {
        if(!typeof(DateTime?).IsAssignableFrom(property.PropertyType)) return null; //We can't convert other types
        return new TextBoxCell{
            Binding = Binding.Delegate((object o) => {
                var dateTime = (property.GetValue(o) as DateTime?);
                if(dateTime.HasValue)
                    return dateTime.Value.ToString("yyyy-MM-dd");
                return "No Date Specified";
            })
        };
    }
}

var app = new Application(new Eto.GtkSharp.Platform());
var converters = new IGridCellConverter[] {new DateTimeToGridCellConverter()}.Concat(GridViewGenerator.GetDefaultGridCellConverters());
var gridView = new GridViewGenerator(converters).GetGridView<MyModel>();
gridView.DataStore = new []{
    new MyModel{
        Title = "My First Model",
        IsChecked = true,
        StringList = new List<string>{
            "First",
            "Second"
        },
        Time = DateTime.Now
    },
    new MyModel{
        Title = "My Second Model",
        IsChecked = null,
        StringList = null,
        Time = null
    }
};
app.Run(new Form {
    Title = "Generator Test",
    Content = gridView
})

Result:

CustomConverter

⚠️ **GitHub.com Fallback** ⚠️