那位大侠有 ItemsControl 类的源码
我最近对在xaml中设置ListBox的Items有些不明白
如下面这个设置:
<ListBox Height="213" Name="listBox1" Width="214" >
<Button Content="Button" Height="72" Width="160" />
<Button Content="Button" Height="72" Width="160" />
<Button Content="Button" Height="72" Width="160" />
<Button Content="Button" Height="72" Width="160" />
</ListBox>
它怎么就给 ListBox.Items 添加了呢,我看到Items是ItemsControl类中变量:
public class ItemsControl : Control
{
.........
public ItemCollection Items { get; }
.........
}
在ItemsControl里面,Items只有get属性,而没set属性,它是怎么被WPF设置的呢?
我如果要自己编写一个像 ItemsControl 控件一样,可以添加子控件到自己的容器Item里面去,该怎么实现呢?
如果哪位大侠有System.Windows.Controls.ItemsControl 此类的实现源码提供,在下将感激不尽。
[解决办法]
// Generated by Reflector from C:\Program Files\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\System.Windows.dll
namespace System.Windows.Controls
{
using MS.Internal;
using MS.Internal.Controls;
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.Security;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls.Primitives;
using System.Windows.Markup;
using System.Windows.Media;
[ContentProperty("Items", true)]
public class ItemsControl : Control, IGeneratorHost
{
private System.Windows.Controls.ItemContainerGenerator _itemContainerGenerator;
private ItemCollection _items;
private DisplayMemberTemplate _oneDisplayMemberTemplate;
public static readonly DependencyProperty DisplayMemberPathProperty = DependencyProperty.RegisterCoreProperty(0x4be9, typeof(string));
private static readonly DependencyProperty IsItemsHostInvalidProperty = DependencyProperty.RegisterCoreProperty(0x4bec, typeof(bool));
internal static readonly DependencyProperty ItemOfGeneratedContainerProperty = DependencyProperty.RegisterAttached("ItemOfGeneratedContainer", typeof(AccessibilityItem), typeof(ItemsControl), new PropertyMetadata(null));
private static readonly DependencyProperty ItemsHostProperty = DependencyProperty.RegisterCoreProperty(0x4bea, typeof(Panel));
public static readonly DependencyProperty ItemsPanelProperty = DependencyProperty.RegisterCoreProperty(0x4beb, typeof(ItemsPanelTemplate));
private static readonly DependencyProperty ItemsProperty = DependencyProperty.RegisterCoreProperty(0x4be7, typeof(ItemCollection));
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(ItemsControl), new PropertyMetadata(new PropertyChangedCallback(ItemsControl.ItemsSourceChanged)));
public static readonly DependencyProperty ItemTemplateProperty = DependencyProperty.RegisterCoreProperty(0x4be8, typeof(DataTemplate));
public ItemsControl() : base(0xc9)
{
base.DefaultStyleKey = typeof(ItemsControl);
}
internal ItemsControl(uint nKnownTypeIndex) : base(nKnownTypeIndex)
{
}
private void AddContainerForPosition(GeneratorPosition position)
{
DependencyObject container = null;
bool isNewlyRealized = false;
using (this.IItemContainerGenerator.StartAt(position, GeneratorDirection.Forward, true))
{
container = this.IItemContainerGenerator.GenerateNext(out isNewlyRealized);
}
int containerIndex = this.ItemContainerGenerator.IndexFromGeneratorPosition(position, false);
this.AddVisualChild(containerIndex, container, isNewlyRealized);
}
private void AddContainers()
{
if (((this.ItemsHost != null) && !this.IsItemsHostInvalid) && (!(this.ItemsHost is VirtualizingPanel) && (this.Items.Count != 0)))
{
using (this.IItemContainerGenerator.StartAt(new GeneratorPosition(-1, 0), GeneratorDirection.Forward, true))
{
for (int i = 0; i < this.Items.Count; i++)
{
bool isNewlyRealized = false;
DependencyObject container = this.IItemContainerGenerator.GenerateNext(out isNewlyRealized);
this.AddVisualChild(i, container, true);
}
}
}
}
private void AddVisualChild(int containerIndex, DependencyObject container, bool needPrepareContainer)
{
UIElementCollection children = this.ItemsHost.Children;
if (containerIndex < children.Count)
{
children.Insert(containerIndex, container as UIElement);
}
else
{
children.Add(container as UIElement);
}
if (needPrepareContainer)
{
this.IItemContainerGenerator.PrepareItemContainer(container);
}
}
internal virtual void ApplyItemContainerStyle(DependencyObject container, object item)
{
}
protected virtual void ClearContainerForItemOverride(DependencyObject element, object item)
{
if (element is UIElement)
{
((UIElement) element).InvalidateAutomationPeer();
}
}
private void ClearContainers(bool bHostIsReplaced)
{
this.IsItemsHostInvalid = true;
if (this.ItemsHost != null)
{
System.Windows.Controls.ItemContainerGenerator itemContainerGenerator = this.ItemContainerGenerator;
if (itemContainerGenerator != null)
{
itemContainerGenerator.StopAnimations();
}
VirtualizingPanel itemsHost = this.ItemsHost as VirtualizingPanel;
if (itemsHost == null)
{
this.ItemsHost.Children.Clear();
}
else if (bHostIsReplaced)
{
itemsHost.Children.Clear();
itemsHost.OnClearChildrenInternal();
}
else if (this.Items.Count > 0)
{
this.ItemContainerGenerator.Refresh();
}
}
}
[AllowReversePInvokeCalls, SecurityCritical]
internal static uint ClearVisualChildren(IntPtr unmanagedObj, bool bHostIsReplaced)
{
try
{
(ManagedPeerTable.GetManagedPeer(unmanagedObj) as ItemsControl).ClearContainers(bHostIsReplaced);
}
catch (Exception exception)
{
return Error.GetXresultForException(exception);
}
return 0;
}
[AllowReversePInvokeCalls, SecurityCritical]
internal static uint DisplayMemberPathChanged(IntPtr unmanagedObj)
{
try
{
ItemsControl managedPeer = ManagedPeerTable.GetManagedPeer(unmanagedObj) as ItemsControl;
managedPeer._oneDisplayMemberTemplate = null;
}
catch (Exception exception)
{
return Error.GetXresultForException(exception);
}
return 0;
}
protected virtual DependencyObject GetContainerForItemOverride()
{
return new ContentPresenter();
}
internal virtual Style GetItemContainerStyle()
{
return null;
}
internal static AccessibilityItem GetItemOfGeneratedContainer(DependencyObject o)
{
if (o == null)
{
throw new ArgumentNullException("o");
}
return (AccessibilityItem) o.GetValue(ItemOfGeneratedContainerProperty);
}
public static ItemsControl GetItemsOwner(DependencyObject element)
{
ItemsControl control = null;
Panel panel = element as Panel;
if ((panel == null)
[解决办法]
!panel.IsItemsHost)
{
return control;
}
ItemsPresenter templatedParent = panel.TemplatedParent as ItemsPresenter;
if (templatedParent == null)
{
return control;
}
control = templatedParent.TemplatedParent as ItemsControl;
if (control != null)
{
return control;
}
element = templatedParent;
while ((element != null) && !(element is ItemsControl))
{
element = VisualTreeHelper.GetParent(element);
}
return (element as ItemsControl);
}
protected virtual bool IsItemItsOwnContainerOverride(object item)
{
return (item is UIElement);
}