Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue mapping view to model in SelectableFilterCollection #2715

Merged
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
11 changes: 8 additions & 3 deletions src/Eto/Forms/FilterCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,20 @@ protected override void Rebuild()
viewToModel = new Dictionary<T, int>(Count);
for (int i = 0; i < Count; i++)
{
// used for lookup below, this index is not correct yet
viewToModel.Add(this[i], i);
}

modelToView = new Dictionary<int, int>(Count);
for (int i = 0; i < Items.Count; i++)
for (int j = 0; j < Items.Count; j++)
{
int index;
if (viewToModel.TryGetValue(Items[i], out index))
modelToView.Add(i, index);
if (viewToModel.TryGetValue(Items[j], out index))
{
modelToView.Add(j, index);
// update index with real index in model
viewToModel[Items[j]] = j;
}
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Eto.Test.UnitTests.Forms.Controls
/// <copyright>(c) 2014 by Vivek Jhaveri</copyright>
/// <license type="BSD-3">See LICENSE for full terms</license>
[TestFixture]
public class GridViewSelectableFilterTests
public class GridViewSelectableFilterTests : TestBase
{
GridView grid;
ObservableCollection<DataItem> model;
Expand All @@ -21,7 +21,7 @@ public class GridViewSelectableFilterTests
[SetUp]
public void Setup()
{
TestBase.Invoke(() =>
Invoke(() =>
{
grid = new GridView();
// Some platforms need at least one column for selection to work
Expand All @@ -40,7 +40,7 @@ public void Setup()
[Test]
public void InsertItemShouldNotChangeSelection()
{
TestBase.Invoke(() =>
Invoke(() =>
{
grid.SelectRow(0);
var selectedItem = grid.SelectedItem;
Expand All @@ -52,7 +52,7 @@ public void InsertItemShouldNotChangeSelection()
[Test]
public void DeleteSelectedItemsShouldRemoveSelectedItems()
{
TestBase.Invoke(() =>
Invoke(() =>
{
grid.AllowMultipleSelection = true;

Expand Down Expand Up @@ -88,7 +88,7 @@ public void DeleteSelectedItemsShouldRemoveSelectedItems()
[TestCase(2)]
public void SortItemsShouldNotChangeSelection(int rowToSelect)
{
TestBase.Invoke(() =>
Invoke(() =>
{
filtered.Sort = GridViewUtils.SortItemsAscending;
grid.SelectRow(rowToSelect);
Expand All @@ -111,7 +111,7 @@ public void SortItemsShouldNotChangeSelection(int rowToSelect)
[Test]
public void FilterItemsShouldNotChangeSelection()
{
TestBase.Invoke(() =>
Invoke(() =>
{
grid.AllowMultipleSelection = true;

Expand All @@ -137,5 +137,47 @@ public void FilterItemsShouldNotChangeSelection()
Assert.AreEqual(0, modelSelectionChangedCount, "Model SelectionChanged event should not fire when changing filter");
});
}

[Test]
public void SortedCollectionShouldGetCorrectRow()
{
GridView grid = null;
Shown(form =>
{
grid = new GridView { Size = new Size(200, 200) };
var collection = new SelectableFilterCollection<GridItem>(grid)
{
new("Hello"),
new("There"),
new("Fine"),
new("World"),
new("Of"),
new("Eto")
};

grid.Columns.Add(new GridColumn
{
DataCell = new TextBoxCell(0)
});
grid.DataStore = collection;
collection.Sort = (x, y) => string.Compare((string)x.Values[0], (string)y.Values[0], StringComparison.Ordinal);
// goes to this order:
// Eto
// Fine
// Hello
// Of
// There
// World

collection.SelectRow(4);
form.Content = grid;
}, () => {

Assert.AreEqual(3, grid.SelectedRow, "#1");
Assert.NotNull(grid.SelectedItem, "#2");
Assert.AreEqual("Of", ((GridItem)grid.SelectedItem).Values[0], "#3");
});
}

}
}
Loading