在ItemsControl中插入项目

时间:2015-10-16 18:48:19

标签: wpf xaml silverlight winrt-xaml itemscontrol

以下是要求。

在我的{{1}}中(如果它可以帮助您考虑该方案,则可以使用{{1}})。我想注入而不是记录,而是将唯一的DataTemplate注入我列表中的任意索引位置。

例如,我可能想将它插入第一个位置,索引0或第三个位置,索引2,或者甚至可能有逻辑将其插入到最后一个位置,索引count-1。

我将需要子类ListView来实现这一点,我意识到。话虽这么说,我可以轻松创建SpecialItemTemplate,SpecialItemIndex DP属性来获取值。

增加了要求:

  1. 不需要特殊类型的收藏
  2. 不需要操纵现有数据
  3. 允许IsHitTestVisible也是可变的
  4. 如何完成此专长的任何想法(在WinRT中)?

1 个答案:

答案 0 :(得分:1)

这是一个基本上是BehaviorTemplate属性的解决方案,可以附加到任何ItemsControl。我使用虚拟化和非虚拟化面板对其进行了测试,适用于这两种情况。如果你认为代码是错综复杂的......我可以不同意,不久就写下来,我不记得我必须按照最终的方式写出来的原因。

用法:

<ListBox ItemsSource="{Binding Persons}">

    <Interactivity:Interaction.Behaviors>
        <AlternativeItemTemplate Index="42">
            <DataTemplate>
                ...foo...
            </DataTemplate>
        </AlternativeItemTemplate>
    </Interactivity:Interaction.Behaviors>

    <ListBox.ItemTemplate>
        <DataTemplate>
            ...bar...
        </DataTemplate>
    </ListBox.ItemTemplate>

</ListBox>

和班级:

[ContentProperty( "ItemTemplate" )]
public class AlternativeItemTemplate : ItemContainerDecorator
{
    public DataTemplate ItemTemplate
    {
        get { return (DataTemplate) GetValue( ItemTemplateProperty ); }
        set { SetValue( ItemTemplateProperty, value ); }
    }

    public static readonly DependencyProperty ItemTemplateProperty =
        DependencyProperty.Register( "ItemTemplate", typeof( DataTemplate ), typeof( AlternativeItemTemplate ), new PropertyMetadata( null ) );

    public int Index
    {
        get { return (int) GetValue( IndexProperty ); }
        set { SetValue( IndexProperty, value ); }
    }

    public static readonly DependencyProperty IndexProperty =
        DependencyProperty.Register( "Index", typeof( int ), typeof( AlternativeItemTemplate ), new PropertyMetadata( -1 ) );

    protected override void OnContainersChanged()
    {
        if (!AssociatedObject.Items.Any() || Index < 0 || Index >= AssociatedObject.Items.Count)
        {
            ItemContentPresenter = null;
            ItemContentControl = null;
            m_overwrittenTemplate = null;
            return;
        }

        TryUpdateItem( ItemContainerGenerator.ContainerFromItem( AssociatedObject.Items[Index] ) );
    }

    private ContentPresenter ItemContentPresenter { get; set; }
    private ContentControl ItemContentControl { get; set; }
    private DataTemplate m_overwrittenTemplate;

    private void TryUpdateItem( DependencyObject itemContainer )
    {
        if (itemContainer == null)
        {
            ResetOverwrittenTemplate();
        }

        var containerAsPresenter = itemContainer as ContentPresenter;
        if (containerAsPresenter != null) UpdateItemContentPresenter( containerAsPresenter );
        else
        {
            var containerAsControl = itemContainer as ContentControl;
            if (containerAsControl != null) UpdateItemContentControl( containerAsControl );
        }
    }

    private void ResetOverwrittenTemplate()
    {
        if (ItemContentPresenter != null)
            ItemContentPresenter.ContentTemplate = m_overwrittenTemplate;

        if (ItemContentControl != null)
            ItemContentControl.ContentTemplate = m_overwrittenTemplate;

        ItemContentPresenter = null;
        ItemContentControl = null;

        m_overwrittenTemplate = null;
    }

    private void UpdateItemContentPresenter( ContentPresenter container )
    {
        if (ItemContentPresenter != null)
            ItemContentPresenter.ContentTemplate = m_overwrittenTemplate;

        ItemContentPresenter = container;
        m_overwrittenTemplate = ItemContentPresenter.ContentTemplate;
        ItemContentPresenter.ContentTemplate = ItemTemplate;
    }

    private void UpdateItemContentControl( ContentControl container )
    {
        if (ItemContentControl != null)
            ItemContentControl.ContentTemplate = m_overwrittenTemplate;

        ItemContentControl = container;
        m_overwrittenTemplate = ItemContentControl.ContentTemplate;
        ItemContentControl.ContentTemplate = ItemTemplate;
    }
}

public abstract class ItemContainerDecorator : Behavior<ItemsControl>
{
    private Dictionary<object, DependencyObject> LastKnownContainers = new Dictionary<object, DependencyObject>();

    protected ItemContainerGenerator ItemContainerGenerator { get { return (AssociatedObject != null) ? AssociatedObject.ItemContainerGenerator : null; } }

    protected override void OnAttached()
    {
        base.OnAttached();
        ItemContainerGenerator.ItemsChanged += HandleItemsChangedInitially;

        if (!TryAddObservers())
        {
            AssociatedObject.Loaded += AddObserversOnLoaded;
        }

        AssociatedObject.Loaded += OnItemsControlLoaded;
        AssociatedObject.LayoutUpdated += OnItemsControlLayoutUpdated;

        CheckContainersChanged();
    }

    private void OnItemsControlLayoutUpdated(object sender, EventArgs eventArgs)
    {
        CheckContainersChanged();
    }

    private void OnItemsControlLoaded(object sender, RoutedEventArgs e)
    {
        CheckContainersChanged();
    }

    private void AddObserversOnLoaded( object sender, RoutedEventArgs e )
    {
        AssociatedObject.Loaded -= AddObserversOnLoaded;
        TryAddObservers();
    }

    private bool TryAddObservers()
    {
        const bool success = true;
        Panel itemsHost =
            AssociatedObject.GetVisualDescendants().OfType<Panel>().FirstOrDefault( panel => panel.IsItemsHost );

        if (itemsHost != null)
        {
            var virtualizingItemsHost = itemsHost as VirtualizingPanel;
            if (virtualizingItemsHost != null)
            {
                virtualizingItemsHost.LayoutUpdated += OnVirtualizingItemsHostLayoutUpdated;
                m_virtualizingItemsHost = virtualizingItemsHost;
            }
            return success;
        }
        return !success;
    }

    private VirtualizingPanel m_virtualizingItemsHost;
    private bool LayoutUpdatedOccurredFirst;

    private void OnVirtualizingItemsHostLayoutUpdated( object sender, EventArgs eventArgs )
    {
        LayoutUpdatedOccurredFirst = true;
        CheckContainersChanged();
    }

    protected override void OnDetaching()
    {
        ItemContainerGenerator.ItemsChanged -= HandleItemsChangedInitially;
        ItemContainerGenerator.ItemsChanged -= HandleItemsChanged;

        AssociatedObject.Loaded -= OnItemsControlLoaded;
        AssociatedObject.LayoutUpdated -= OnItemsControlLayoutUpdated;

        AssociatedObject.Loaded -= AddObserversOnLoaded;
        if (m_virtualizingItemsHost != null) m_virtualizingItemsHost.LayoutUpdated -= OnVirtualizingItemsHostLayoutUpdated;
        m_virtualizingItemsHost = null;

        base.OnDetaching();
    }

    private void HandleItemsChangedInitially( object sender, ItemsChangedEventArgs e )
    {
        ItemContainerGenerator.ItemsChanged -= HandleItemsChangedInitially;

        if (!LayoutUpdatedOccurredFirst)
        {

            //sometimes calling UpdateLayout throws an ArgumentException
            //don't know why so we just swallow it
            //it's not particularly important
            try
            {
                AssociatedObject.UpdateLayout();
            }
            catch (ArgumentException) { }
        }

        ItemContainerGenerator.ItemsChanged += HandleItemsChanged;
        CheckContainersChanged();
    }

    private void HandleItemsChanged( object sender, ItemsChangedEventArgs e )
    {
        CheckContainersChanged();
    }

    private void CheckContainersChanged()
    {
        var newestContainers = new Dictionary<object, DependencyObject>();
        foreach (var item in AssociatedObject.Items)
        {
            newestContainers[item] = ItemContainerGenerator.ContainerFromItem( item );
        }

        if (!LastKnownContainers.SequenceEqual( newestContainers ))
        {
            LastKnownContainers = newestContainers;
            OnContainersChanged();
        }
    }

    protected abstract void OnContainersChanged();
}