您正在创建自定义控件的图像,其行为类似于 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) ItemsSource
至DelayedItemsSource
在你延迟之后。
关于wpf - ItemsControl ItemsSource 延迟加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7597456/