CustomCell in Eto.Forms

Hi guys (especially @curtisw and @dale)
I have a question related to CustomCell.
I am working with TreeGridView, where one column is defined as CustomCell (containing only button so far).
Now, when adding TreeGridItem I am using SetValue method to assign a value of certain column.
For TextBoxCell it is just like: mytgi.SetValue(1,"SomeData").
How to assign value to CustomCell? Should I use Create method of CustomCell?
Any short example would be highly appreciated.

Kind regards,
Dmitriy

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

Thanks, @curtisw
Works perfectly.

1 Like

Good evening,

works perfect for a textbox, but I cant get it to work with a checkbox. Binding the checkbox.Checked property to a property in the viewmodel.

checkBox.BindDataContext(c => c.Checked, Binding.Property((AFGridItem m) => m.BooleanProperty));

I also tried:

checkBox.CheckedBinding.BindDataContext((AFGridItem m) => m.BooleanProperty);

When clicking the checkbox the property never gets set and remains NULL.

Any help would be much appreciated.

Thanks
Christian

Alright,

I changed the code and it seems to work:

protected override Control OnCreateCell(CellEventArgs args)
{
    if (args.Item is AFGridItem item)
    {
        CheckBox checkBox = new CheckBox
        {
            Enabled = true,
            ThreeState = true,
        };
        if (this.ViewModel.DataSet.IsLocked)
        {
            checkBox.Enabled = false;
        }
        checkBox.CheckedBinding.Bind(item, i => i.BooleanProperty);
        return checkBox;
    }
    else
    {
        return new Label
        {
            Wrap = WrapMode.None,
        };
    }
}

Kindest
Christian

Would it be possible to get for the CustomCell in the Methods OnCommitEdit, OnCancelEdit and OnBeginEdit not only the CellEventArgs but also the embedded Control, same as OnConfigureCell? The CustomCell has a property Control, but it is null. Is there a way to assign to this (I see a protected set;)?

Kindest
Christian

Just getting back to this - it would make things so much easier for me, if I could get access to the control also in the OnCommitEdit , OnCancelEdit and OnBeginEdit.

Any suggestions on this @dale or @curtisw?

Kindest
Christian

1 Like

There’s no way currently to access the control in those events, but I can take a look to see if it can be done. I’ve created RH-69687 to tackle that.

Thanks for the reply. Just for my understanding, the CellEventArgs have a Control property. What is this control if not the custom control you specified in the OnCreateCell?

Kindest
Christian