Skip to content

Commit

Permalink
update to repeater to allow passing in uielements in itemssource
Browse files Browse the repository at this point in the history
* minor update to repeater to allow passing in uielements in itemssource

* minor fixes

Ported from microsoft/microsoft-ui-xaml@95d5ff0
  • Loading branch information
grokys committed Jan 9, 2020
1 parent 08c9710 commit 4cca4f0
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions src/Avalonia.Controls/Repeater/ViewManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,19 @@ public void ClearElement(IControl element, bool isClearedDueToCollectionChange)

public void ClearElementToElementFactory(IControl element)
{
var virtInfo = ItemsRepeater.GetVirtualizationInfo(element);
var clearedIndex = virtInfo.Index;
_owner.OnElementClearing(element);
_owner.ItemTemplateShim.RecycleElement(_owner, element);
_owner.ItemTemplateShim?.RecycleElement(_owner, element);

var virtInfo = ItemsRepeater.GetVirtualizationInfo(element);
virtInfo.MoveOwnershipToElementFactory();

if (_lastFocusedElement == element)
{
// Focused element is going away. Remove the tracked last focused element
// and pick a reasonable next focus if we can find one within the layout
// realized elements.
MoveFocusFromClearedIndex(clearedIndex);
MoveFocusFromClearedIndex(virtInfo.Index);
}

}

private void MoveFocusFromClearedIndex(int clearedIndex)
Expand Down Expand Up @@ -518,27 +516,44 @@ private IControl GetElementFromPinnedElements(int index)
private IControl GetElementFromElementFactory(int index)
{
// The view generator is the provider of last resort.

var data = _owner.ItemsSourceView.GetAt(index);
var itemTemplateFactory = _owner.ItemTemplateShim;
IControl element = null;
var itemsSourceContainsElements = false;

if (itemTemplateFactory == null)
{
// If no ItemTemplate was provided, use a default
var factory = FuncDataTemplate.Default;
_owner.ItemTemplate = factory;
itemTemplateFactory = _owner.ItemTemplateShim;
element = data as IControl;

// No item template provided and ItemsSource contains objects derived from UIElement.
// In this case, just use the data directly as elements.
itemsSourceContainsElements = element != null;
}

var data = _owner.ItemsSourceView.GetAt(index);
var element = itemTemplateFactory.GetElement(_owner, data);
if (element == null)
{
if (itemTemplateFactory == null)
{
// If no ItemTemplate was provided, use a default
var factory = FuncDataTemplate.Default;
_owner.ItemTemplate = factory;
itemTemplateFactory = _owner.ItemTemplateShim;
}

element = itemTemplateFactory.GetElement(_owner, data);
}

var virtInfo = ItemsRepeater.TryGetVirtualizationInfo(element);
if (virtInfo == null)
{
virtInfo = ItemsRepeater.CreateAndInitializeVirtualizationInfo(element);
}

// Prepare the element
element.DataContext = data;
if (!itemsSourceContainsElements)
{
// Prepare the element
element.DataContext = data;
}

virtInfo.MoveOwnershipToLayoutFromElementFactory(
index,
Expand Down

0 comments on commit 4cca4f0

Please sign in to comment.