Skip to content

Commit

Permalink
Merge pull request #3251 from MarchingCube/fix-itemsrepeater-itemsour…
Browse files Browse the repository at this point in the history
…ce-reset

Fix ItemsRepeater not clearing focused elements on items source changed.
  • Loading branch information
grokys authored Nov 13, 2019
2 parents 82f1ec8 + 7f72230 commit 9295858
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
3 changes: 2 additions & 1 deletion samples/ControlCatalog/Pages/ItemsRepeaterPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</ComboBox>
<Button Command="{Binding AddItem}">Add Item</Button>
<Button Command="{Binding RandomizeHeights}">Randomize Heights</Button>
<Button Command="{Binding ResetItems}">Reset items</Button>
</StackPanel>
<Border BorderThickness="1" BorderBrush="{DynamicResource ThemeBorderMidBrush}" Margin="0 0 0 16">
<ScrollViewer Name="scroller"
Expand All @@ -23,7 +24,7 @@
<ItemsRepeater Name="repeater" Background="Transparent" Items="{Binding Items}">
<ItemsRepeater.ItemTemplate>
<DataTemplate>
<TextBlock Height="{Binding Height}" Text="{Binding Text}"/>
<TextBlock Focusable="True" Height="{Binding Height}" Text="{Binding Text}"/>
</DataTemplate>
</ItemsRepeater.ItemTemplate>
</ItemsRepeater>
Expand Down
9 changes: 9 additions & 0 deletions samples/ControlCatalog/Pages/ItemsRepeaterPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public ItemsRepeaterPage()
_repeater = this.FindControl<ItemsRepeater>("repeater");
_scroller = this.FindControl<ScrollViewer>("scroller");
_repeater.PointerPressed += RepeaterClick;
_repeater.KeyDown += RepeaterOnKeyDown;
DataContext = new ItemsRepeaterPageViewModel();
}

Expand Down Expand Up @@ -77,5 +78,13 @@ private void RepeaterClick(object sender, PointerPressedEventArgs e)
var item = (e.Source as TextBlock)?.DataContext as ItemsRepeaterPageViewModel.Item;
((ItemsRepeaterPageViewModel)DataContext).SelectedItem = item;
}

private void RepeaterOnKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.F5)
{
((ItemsRepeaterPageViewModel)DataContext).ResetItems();
}
}
}
}
36 changes: 28 additions & 8 deletions samples/ControlCatalog/ViewModels/ItemsRepeaterPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,27 @@ namespace ControlCatalog.ViewModels
{
public class ItemsRepeaterPageViewModel : ReactiveObject
{
private int newItemIndex = 1;
private int _newItemIndex = 1;
private int _newGenerationIndex = 0;
private ObservableCollection<Item> _items;

public ItemsRepeaterPageViewModel()
{
Items = new ObservableCollection<Item>(
Enumerable.Range(1, 100000).Select(i => new Item
{
Text = $"Item {i.ToString()}",
}));
Items = CreateItems();
}

public ObservableCollection<Item> Items { get; }
public ObservableCollection<Item> Items
{
get => _items;
set => this.RaiseAndSetIfChanged(ref _items, value);
}

public Item SelectedItem { get; set; }

public void AddItem()
{
var index = SelectedItem != null ? Items.IndexOf(SelectedItem) : -1;
Items.Insert(index + 1, new Item { Text = $"New Item {newItemIndex++}" });
Items.Insert(index + 1, new Item { Text = $"New Item {_newItemIndex++}" });
}

public void RandomizeHeights()
Expand All @@ -38,6 +40,24 @@ public void RandomizeHeights()
}
}

public void ResetItems()
{
Items = CreateItems();
}

private ObservableCollection<Item> CreateItems()
{
var suffix = _newGenerationIndex == 0 ? string.Empty : $"[{_newGenerationIndex.ToString()}]";

_newGenerationIndex++;

return new ObservableCollection<Item>(
Enumerable.Range(1, 100000).Select(i => new Item
{
Text = $"Item {i.ToString()} {suffix}"
}));
}

public class Item : ReactiveObject
{
private double _height = double.NaN;
Expand Down
12 changes: 11 additions & 1 deletion src/Avalonia.Controls/Repeater/ItemsRepeater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,17 @@ private void OnDataSourcePropertyChanged(ItemsSourceView oldValue, ItemsSourceVi
if (Layout is VirtualizingLayout virtualLayout)
{
var args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
virtualLayout.OnItemsChanged(GetLayoutContext(), newValue, args);

_processingItemsSourceChange = args;

try
{
virtualLayout.OnItemsChanged(GetLayoutContext(), newValue, args);
}
finally
{
_processingItemsSourceChange = null;
}
}
else if (Layout is NonVirtualizingLayout nonVirtualLayout)
{
Expand Down

0 comments on commit 9295858

Please sign in to comment.