BaconographyWP8.Common.ReorderListBox.PrepareContainerForItemOverride C# (CSharp) Method

PrepareContainerForItemOverride() protected method

Ensures that a possibly-recycled item container (ReorderListBoxItem) is ready to display a list item.
protected PrepareContainerForItemOverride ( DependencyObject element, object item ) : void
element System.Windows.DependencyObject
item object
return void
        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            base.PrepareContainerForItemOverride(element, item);

            ReorderListBoxItem itemContainer = (ReorderListBoxItem)element;
            itemContainer.ApplyTemplate();  // Loads visual states.

            // Set this state before binding to avoid showing the visual transition in this case.
            string reorderState = this.IsReorderEnabled ?
                ReorderListBoxItem.ReorderEnabledState : ReorderListBoxItem.ReorderDisabledState;
            VisualStateManager.GoToState(itemContainer, reorderState, false);

            itemContainer.SetBinding(ReorderListBoxItem.IsReorderEnabledProperty,
                new Binding(ReorderListBox.IsReorderEnabledPropertyName) { Source = this });

            if (item == this.dragItem)
            {
                itemContainer.IsSelected = this.isDragItemSelected;
                VisualStateManager.GoToState(itemContainer, ReorderListBoxItem.DraggingState, false);

                if (this.dropTargetIndex >= 0)
                {
                    // The item's dragIndicator is currently being moved, so the item itself is hidden. 
                    itemContainer.Visibility = Visibility.Collapsed;
                    this.dragItemContainer = itemContainer;
                }
                else
                {
                    itemContainer.Opacity = 0;
                    this.Dispatcher.BeginInvoke(() => this.AnimateDrop(itemContainer));
                }
            }
            else
            {
                VisualStateManager.GoToState(itemContainer, ReorderListBoxItem.NotDraggingState, false);
            }
        }