CustomCell in Eto.Forms

Hey @Dmitriy,

With the CustomCell you can use (On)ConfigureCell or BindDataContext to hook up the values in the TreeGridItem to the controls.

A sample using OnConfigureCell would look like this:

class MyCustomButtonCellWithConfigure : CustomCell
{
	protected override Control OnCreateCell(CellEventArgs args)
	{
		return new Button();
	}

	protected override void OnConfigureCell(CellEventArgs args, Control control)
	{
		if (control is Button button && args.Item is TreeGridItem treeItem)
		{
			button.Text = treeItem.GetValue(1) as string;
		}

		base.OnConfigureCell(args, control);
	}
}

The drawbacks of this approach is that when your tree node updates, it won’t update the cell value. A way to do that is to use data binding, like so:

class MyTreeNode : TreeGridItem, INotifyPropertyChanged
{
	string _someTextProperty;
	public string SomeTextProperty
	{
		get => _someTextProperty;
		set
		{
			if (_someTextProperty != value)
			{
				_someTextProperty = value;
				PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SomeTextProperty)));
			}
		}
	}

	public event PropertyChangedEventHandler PropertyChanged;
}

class MyCustomButtonCellWithBinding : CustomCell
{
	protected override Control OnCreateCell(CellEventArgs args)
	{
		var button = new Button();
		button.BindDataContext(c => c.Text, Binding.Property((MyTreeNode m) => m.SomeTextProperty));
		return button;
	}
}

Using data binding can also be much more versatile when you have more than one control or property that you are binding values to.

Hope this helps!

Cheers,
Curtis.

1 Like