wpf - ItemsControl ItemsSource 延迟加载

标签 wpf lazy-loading selector itemscontrol itemsource

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

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

System.Windows.Controls.Primitives.Selector

类(class)。
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 (而是延迟它)。

在你看来应该怎么做?

谢谢

最佳答案

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

关于wpf - ItemsControl ItemsSource 延迟加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7597456/

相关文章:

c# - ObservableCollection 和 INotifyPropertyChanged 的​​ WPF MVVM 问题

java - 为什么在 Struts 1.2.7 中延迟实例化 MessageResourcesFactory?

angular - 延迟加载 : Observable not subscribed

jquery - 单击这个或那个,然后执行某些操作

Java NIO 数据包粘连

jquery - 使用 jQuery 设置选择

wpf - 在模型或 View 模型上实现INotifyPropertyChanged?

c# - System.Timers 定时器不触发事件

c# - 如何将 wpf 调度程序转换为 winforms

不使用 Angular CLI 的 Angular 2 AOT 和延迟加载