Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Avalonia.Controls;
using Avalonia.Controls.Models;
using Avalonia.Controls.Models.TreeDataGrid;
using Avalonia.Controls.Selection;
using TreeDataGridDemo.Models;

namespace TreeDataGridDemo.ViewModels
Expand All @@ -13,7 +14,7 @@ public DynamicColumnsViewModel()
var _data = DataRow.CreateRandomItems();

Source = new HierarchicalTreeDataGridSource<DataRow>(_data);
Source.RowSelection!.SingleSelect = true;
Source.Selection = new TreeDataGridCellSelectionModel<DataRow>(Source);

ColumnCount = 20;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ sender.Rows is null ||
sender.RowsPresenter?.BringIntoView(
rowIndex,
sender.ColumnHeadersPresenter?.TryGetElement(columnIndex)?.Bounds);

if (sender.TryGetCell(columnIndex, rowIndex) is { Focusable: true} targetCell)
targetCell.Focus();
}

void ITreeDataGridSelectionInteraction.OnPointerPressed(TreeDataGrid sender, PointerPressedEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System.Linq;
using Avalonia.Collections;
using Avalonia.Controls.Models.TreeDataGrid;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Selection;
using Avalonia.Headless;
using Avalonia.Headless.XUnit;
using Avalonia.Input;
using Avalonia.Styling;
using Avalonia.Threading;
using Avalonia.VisualTree;
using Xunit;

namespace Avalonia.Controls.TreeDataGridTests.Selection;

public class TreeDataGridCellSelectionTests
{
[AvaloniaFact]
public void F2_Should_Edit_Current_Cell_After_Arrow_Navigation()
{
var target = CreateTarget();
var source = (FlatTreeDataGridSource<Model>)target.Source!;
var cellSelection = new TreeDataGridCellSelectionModel<Model>(source)
{
SingleSelect = true
};
source.Selection = cellSelection;

target.GetVisualDescendants()
.OfType<TreeDataGridTextCell>()
.First(x => x.RowIndex == 0)
.Focus();

cellSelection.SetSelectedRange(new(0, new(0)), 1, 1);
Assert.Single(cellSelection.SelectedIndexes);
Assert.Equal(0, cellSelection.SelectedIndexes[0].RowIndex);

target.RaiseEvent(new KeyEventArgs
{
RoutedEvent = InputElement.KeyDownEvent,
Key = Key.Down,
});

Assert.Single(cellSelection.SelectedIndexes);
Assert.Equal(1, cellSelection.SelectedIndexes[0].RowIndex);

target.RaiseEvent(new KeyEventArgs
{
RoutedEvent = InputElement.KeyDownEvent,
Key = Key.F2,
});

var editingElement = target.GetVisualDescendants()
.OfType<TreeDataGridTextCell>()
.FirstOrDefault(x => x.IsFocused && x.IsVisible);

Assert.NotNull(editingElement);
Assert.Equal(1, editingElement.RowIndex);
}

private static TreeDataGrid CreateTarget(
int itemCount = 10,
bool runLayout = true)
{
AvaloniaList<Model>? items = [.. Enumerable.Range(0, itemCount).Select(x =>
new Model
{
Title = "Item " + x,
})];


var source = new FlatTreeDataGridSource<Model>(items);
source.Columns.Add(new TextColumn<Model, string?>("Title", x => x.Title, (o, v) => o.Title = v));

var target = new TreeDataGrid
{
Template = TestTemplates.TreeDataGridTemplate(),
Source = source,
};

var root = new TestWindow(target)
{
Styles =
{
new Style(x => x.Is<TreeDataGridRow>())
{
Setters =
{
new Setter(TreeDataGridRow.TemplateProperty, TestTemplates.TreeDataGridRowTemplate()),
}
},
}
};

if (runLayout)
{
root.UpdateLayout();
Dispatcher.UIThread.RunJobs();
}

return target;
}

private class Model
{
public string? Title { get; set; }
}
}
Loading