protected override void PrepareContainerForItemOverride(DependencyObject element, object item) { RemovableItemContentControl control = element as RemovableItemContentControl; control.ContentTemplate = ItemTemplate; control.Content = item; itemsSourceHelper.RegisterItemContainerPair(item, element as FrameworkElement); int itemIndex = itemsSourceHelper.ItemIndex(item); if (itemIndex == 0) { if (itemsSourceHelper.ItemsCount() > 1) { control.Margin = ItemsControlHelper.GetFirstItemMargin(this); } else { control.Margin = ItemsControlHelper.GetLastItemMargin(this); } } else if (itemsSourceHelper.HasItems() && itemIndex + 1 < itemsSourceHelper.ItemsCount()) { control.Margin = ItemsControlHelper.GetRegularItemMargin(this); } else { control.Margin = ItemsControlHelper.GetLastItemMargin(this); } if (itemsSourceHelper.GetPreviousContainer(item) is RemovableItemContentControl previous) { previous.Margin = ItemsControlHelper.GetRegularItemMargin(this); } base.PrepareContainerForItemOverride(element, item); }