1.ItemsControl是控件嗎
ItemsControl可以在Panel中直接使用
<StackPanel>
<ItemsControl Name="items">
</ItemsControl>
</StackPanel>
直接設定ItemsSource也是可以顯示的:
items.ItemsSource = new List<string> {"A","B","C" };
說明ItemsControl和普通的控件是相同的。
2.ItemsControl的控件的特性
ItemsControl是一個可以包含多個項目的控件,可以通過設定ItemsSource或者Items來指定ItemsControl的内容
ItemsSource:
ItemsSource可以是任何實作了IEnumerable的類型,比如說ArrayList,HashTable,Queue,List等等
Items:
可以使用Items.Add方法來手動添加ItemsControl子項,可支援各種類型,如:
items.Items.Add(DateTime.Now);
items.Items.Add("String");
Ellipse ellipse = new Ellipse();
ellipse.Width = 40;
ellipse.Height = 40;
ellipse.Fill = Brushes.Blue;
items.Items.Add(ellipse);
顯示的結果如下圖:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5iYtVHa09VZnFWbp9CXyUDN58FbvJHdu92QtVGdJ9CXyVGdpJ3VtUmdpxULzd3bk5WaX9CX0kTeyVGblN2Lc12bj91cn9Gbi52YvwVbvNmLzd2bsJmbj5ycldWYtl2Lc9CX6MHc0RHaiojIsJye.png)
關于ItemsSource和Items的關系:
設定了ItemsSource之後,Items屬性将是隻讀的,且具有固定的大小
如果想恢複使用Items可以将ItemsSource設定為null,此時Items将是一個空的ItemCollection
3.哪些控件繼承了ItemsControl
如上圖包含了以下控件:
Selector:Combox,ListBox,TabControl,MultiSelector
HeaderdItemsControl:MenuItem,ToolBar,TreeViewItem
MenuBase:ContextMenu,Menu
TreeView
StatusBar
DataGridCellsPresenter
DataGridColumnHeaderPresenter
Celery 标簽: WPF
轉載于:https://www.cnblogs.com/celery94/archive/2010/10/25/1860437.html