我最近对在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));