diff --git a/src/Models/TreeDataGridSelectionModel.cs b/src/Models/TreeDataGridSelectionModel.cs index b016d739..e45393d0 100644 --- a/src/Models/TreeDataGridSelectionModel.cs +++ b/src/Models/TreeDataGridSelectionModel.cs @@ -42,23 +42,15 @@ namespace SourceGit.Models public void Select(IEnumerable items) { - var sets = new HashSet(); - foreach (var item in items) - sets.Add(item); - using (BatchUpdate()) { Clear(); - int num = _source.Rows.Count; - for (int i = 0; i < num; ++i) + foreach (var selected in items) { - var m = _source.Rows[i].Model as TModel; - if (m != null && sets.Contains(m)) - { - var idx = _source.Rows.RowIndexToModelIndex(i); + var idx = GetModelIndex(_source.Items, selected, IndexPath.Unselected); + if (!idx.Equals(IndexPath.Unselected)) Select(idx); - } } } } @@ -431,6 +423,30 @@ namespace SourceGit.Models return _childrenGetter?.Invoke(node); } + private IndexPath GetModelIndex(IEnumerable collection, TModel model, IndexPath parent) + { + int i = 0; + + foreach (var item in collection) + { + var index = parent.Append(i); + if (item != null && item == model) + return index; + + var children = GetChildren(item); + if (children != null) + { + var findInChildren = GetModelIndex(children, model, index); + if (!findInChildren.Equals(IndexPath.Unselected)) + return findInChildren; + } + + i++; + } + + return IndexPath.Unselected; + } + private bool HasChildren(IRow row) { var children = GetChildren(row.Model as TModel);