AvaloniaUI / Avalonia.Controls.TreeDataGrid

A combined TreeView/DataGrid for Avalonia.
MIT License
234 stars 49 forks source link

Error "Object reference not set to an instance of an object." if FlatTreeDataGridSource is recreated with changing edit mode #250

Closed Wowhere closed 5 months ago

Wowhere commented 5 months ago

Error "Object reference not set to an instance of an object." if FlatTreeDataGridSource is recreated

I`am trying to change EditMode and ReadOnly modes in treeDataGrid in method TreeDataGridInit (code below). Scenario

  1. Change mode from ReadOnly to Edit (IsGridEditable = true)
  2. Change mode from Edit to ReadOnly (IsGridEditable = false)

Result: "Object reference not set to an instance of an object.". Debugger shows that Hint in TemplateColumn is null. Whats wrong, i cant understand how to avoid this error.

Avalonia 11.0.2. Windows 10 Pro 19045.3930

public void TreeDataGridInit()
{
    var invisible = new GridLength(0);

    if (IsGridEditable)
    {
        var EditOptions= new TextColumnOptions<Hint>
        {
            BeginEditGestures = BeginEditGestures.Tap,
            IsTextSearchEnabled = true
        };

        Source = new FlatTreeDataGridSource<Hint>(Hints)
        {

            Columns =
            {
                //new TextColumn<Hint, int>("Id", x => x.Id, options: new TextColumnOptions<Hint>{MaxWidth = invisible}),
                new TextColumn<Hint, string>("Text", x => x.HintText, (r, v) => r.HintText = v, options: EditOptions),
                new TextColumn<Hint, string>("Comment", x => x.Comment, (r, v) => r.Comment = v, options: EditOptions),
                new TemplateColumn<Hint>("", new FuncDataTemplate<Hint>((a, e) => DeleteButtonInit(a.Id)))
            },
        };
        IsAddButtonVisible = true;
    }
    else
    {
        Source = new FlatTreeDataGridSource<Hint>(Hints)
        {
            Columns =
            {
                new TextColumn<Hint, int>("Id", x => x.Id, options: new TextColumnOptions<Hint>{MaxWidth = invisible}),
                new TextColumn<Hint, string>("Text", x => x.HintText),
                new TextColumn<Hint, string>("Comment", x => x.Comment)
            },
        };
        IsAddButtonVisible = false;
    }
    Source.Selection = new TreeDataGridCellSelectionModel<Hint>(Source) { };
}
Wowhere commented 5 months ago

Error is avoided by new TemplateColumn<Hint>("", new FuncDataTemplate<Hint>((a, e) => DeleteButtonInit(a.Id), supportsRecycling: true)) but i cant understand why its working

Wowhere commented 5 months ago

Whatever