【问题标题】:ItemsControl ItemsSource lazy loadingItemsControl ItemsSource 延迟加载
【发布时间】:2011-11-27 16:30:33
【问题描述】:

图像您正在创建一个自定义控件,其行为类似于 WPF 中的ComboBox。 作为您提供IQueryable<T>(或任何类型的IEnumerable 集合)的项目来源, 但您不想让控件调用GetIterator() 并遍历它(某种延迟加载)。

假设您继承自(因为您想要该控件的所有功能)

System.Windows.Controls.Primitives.Selector

类。 Selector 类继承自 System.Windows.Controls.ItemsControl 类,该类提供众所周知的依赖属性 ItemsSource。

public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(ItemsControl), 
    new FrameworkPropertyMetadata(null, new PropertyChangedCallback(ItemsControl.OnItemsSourceChanged)));

private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    ItemsControl control = (ItemsControl) d;
    IEnumerable oldValue = (IEnumerable) e.OldValue;
    IEnumerable newValue = (IEnumerable) e.NewValue;
    ItemValueStorageField.ClearValue(d);
    if ((e.NewValue == null) && !BindingOperations.IsDataBound(d, ItemsSourceProperty))
    {
        control.Items.ClearItemsSource();
    }
    else
    {
        control.Items.SetItemsSource(newValue); // PROBLEM
    }
    control.OnItemsSourceChanged(oldValue, newValue);
}

如果我没看错,这就是它迭代的地方。

internal void SetItemsSource(IEnumerable value)
{
    if ((!this.IsUsingItemsSource && (this._internalView != null)) && (this._internalView.RawCount > 0))
    {
        throw new InvalidOperationException(SR.Get("CannotUseItemsSource"));
    }
    this._itemsSource = value;
    this._isUsingItemsSource = true;
    this.SetCollectionView(CollectionViewSource.GetDefaultCollectionView(this._itemsSource, this.ModelParent));
}

所以我决定重写 ItemsSourceProperty 的元数据并将其指向我自己的静态方法, 我计划的地方不是联合呼叫SetItemsSource(而是延迟它)。

你认为应该怎么做?

谢谢

【问题讨论】:

    标签: wpf lazy-loading selector itemscontrol itemsource


    【解决方案1】:

    您最好的选择可能是添加一个新的依赖属性,例如IEnumerable 类型的DelayedItemsSource。然后你可以在延迟之后将ItemsSource 绑定到DelayedItemsSource

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 2014-03-06
      • 2021-07-13
      • 2010-11-26
      相关资源
      最近更新 更多