【问题标题】:Binding two different Collection Source in Pivotviewer在 Pivotviewer 中绑定两个不同的集合源
【发布时间】:2012-08-24 22:43:33
【问题描述】:

我正在尝试了解如何在 pivotviewer 中管理两个集合之间的转换。集合具有相同的图像,只是处理了一个集合。我想让一个集合消失在另一个集合中。

我知道如何定义不同的模板,这些模板通过固定最大宽度来消失。例如,一旦您放大 300 像素,然后您将拥有新模板,直到达到 500 像素等。我用来绑定我在后面代码中加载的集合的代码是这样的:

<pv:PivotViewerItemTemplate x:Key="firstTemplate" MaxWidth="300">
    <!-- template layout -->
    <pv:PivotViewerMultiScaleSubImageHost CollectionSource="{Binding [VisualCollectionSource][0] }"  ImageId="{Binding [VisualImageId][0]}" />
    <!-- template layout -->
</pv:PivotViewerItemTemplate>

有没有像this 这样的解决方案我可以采用?它的最佳做法是什么?

【问题讨论】:

  • 您的意思是放大时一个集合应该消失(淡出?)到另一个集合,还是您想在某个时候加载另一个集合在第一个加载之后?
  • 是的,第一个选项正是我想要得到的效果。
  • 没有实现缩放时更改集合,仅动态替换具有某些重叠项的集合。对于第二个选项,按照 Tony Champions'sChris Arnold's 教程/帖子使用 ObservableCollection 非常容易。
  • 谢谢,我知道这两个博客。它们非常有用。希望有人提出一些想法。
  • 实际上,我正在尝试按照您的建议加载另一个集合,但我无法成功。对于我实际需要的这个解决方案将起作用。您能否提供一个简单的示例作为答案?

标签: c# silverlight data-binding silverlight-5.0 pivotviewer


【解决方案1】:

这是一个保持加载的 CXML 集合之间重叠的示例,而不是替换整个集合。由于添加和删除对象时有动画,所以看起来很不错。从服务器/后端请求更多/部分数据时很有用。 (当然,这与缩放时“褪色的集合/项目”无关。)

最有趣的代码在KeepIntersection(this ICollection&lt;PivotViewerItem&gt; currentItems, CxmlCollectionSource newItems) 中,它通过仅添加和删除旧集合和新集合的差异来修改集合。

根据 Tony ChampionsChris Arnold 的教程/帖子,基于 Silverlight 5 PivotViewer 的 ObservableCollection

MainPageViewModel.cs

private void CxmlCollectionSource_StateChanged(object sender, CxmlCollectionStateChangedEventArgs e)
{
    // TODO: check other states
    switch (e.NewState)
    {
        case CxmlCollectionState.Loaded:
            {
                var collection = sender as CxmlCollectionSource;

                Debug.Assert(collection != null, "collection != null");

                // TODO: don't add/remove, replace the entire list after diffing
                if (!this.pivotProperties.Any())
                {
                    // TODO: diffing algorithm for properties, minimal changes
                    foreach (var pivotViewerProperty in collection.ItemProperties)
                    {
                        this.pivotProperties.Add(pivotViewerProperty);
                    }
                }

                this.pivotViewerItems.KeepIntersection(collection);

                break;
            }
    }
}

ICollection{PivotViewerItem}Extensions.cs

namespace SilverlightPivotViewer.Extensions
{
    #region Using directives

    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Controls.Pivot;

    #endregion

    public static class ICollectionPivotViewerItemExtensions
    {
        #region Public Methods and Operators

        public static void KeepIntersection(
            this ICollection<PivotViewerItem> currentItems, CxmlCollectionSource newItems)
        {
            RemoveCurrentUniqueItems(currentItems, newItems);

            AddNewUniqueItems(currentItems, newItems);
        }

        #endregion

        #region Methods

        private static void AddNewUniqueItems(ICollection<PivotViewerItem> currentItems, CxmlCollectionSource newItems)
        {
            IEnumerable<PivotViewerItem> onlyInNewCollection =
                newItems.Items.Where(pivotViewerItem => currentItems.All(i => i.Id != pivotViewerItem.Id));

            foreach (var pivotViewerItem in onlyInNewCollection)
            {
                currentItems.Add(pivotViewerItem);
            }
        }

        private static void RemoveCurrentUniqueItems(
            ICollection<PivotViewerItem> currentItems, CxmlCollectionSource newItems)
        {
            IEnumerable<PivotViewerItem> onlyInCurrentCollection =
                currentItems.Where(pivotViewerItem => newItems.Items.All(i => i.Id != pivotViewerItem.Id));

            // Need to produce a list, otherwise it will crash (concurrent looping and editing the IEnumerable, or something related)
            var onlyInCurrentCollectionList = onlyInCurrentCollection.ToList();

            foreach (var pivotViewerItem in onlyInCurrentCollectionList)
            {
                currentItems.Remove(pivotViewerItem);
            }
        }

        #endregion
    }
}
  • 编码的自定义差异函数,但我确信有人有一个很棒的库可以做到这一点。
  • 也曾考虑为构面类别添加差异,但不是为此而构建的。我想推荐的方法是确保客户端知道所有可用的类别,以便它们可以用于过滤。

【讨论】:

  • 谢谢。我会在接下来的几天里尝试深入研究它。
猜你喜欢
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-27
相关资源
最近更新 更多