public ref class ItemsControl : System::Windows::Controls::Control, System::Windows::Markup::IAddChild
public ref class ItemsControl : System::Windows::Controls::Control, System::Windows::Controls::Primitives::IContainItemStorage, System::Windows::Markup::IAddChild
[System.Windows.Localizability(System.Windows.LocalizationCategory.None, Readability=System.Windows.Readability.Unreadable)]
[System.Windows.Markup.ContentProperty("Items")]
[System.Windows.StyleTypedProperty(Property="ItemContainerStyle", StyleTargetType=typeof(System.Windows.FrameworkElement))]
public class ItemsControl : System.Windows.Controls.Control, System.Windows.Markup.IAddChild
[System.Windows.Localizability(System.Windows.LocalizationCategory.None, Readability=System.Windows.Readability.Unreadable)]
[System.Windows.Markup.ContentProperty("Items")]
[System.Windows.StyleTypedProperty(Property="ItemContainerStyle", StyleTargetType=typeof(System.Windows.FrameworkElement))]
public class ItemsControl : System.Windows.Controls.Control, System.Windows.Controls.Primitives.IContainItemStorage, System.Windows.Markup.IAddChild
[<System.Windows.Localizability(System.Windows.LocalizationCategory.None, Readability=System.Windows.Readability.Unreadable)>]
[<System.Windows.Markup.ContentProperty("Items")>]
[<System.Windows.StyleTypedProperty(Property="ItemContainerStyle", StyleTargetType=typeof(System.Windows.FrameworkElement))>]
type ItemsControl = class
inherit Control
interface IAddChild
[<System.Windows.Localizability(System.Windows.LocalizationCategory.None, Readability=System.Windows.Readability.Unreadable)>]
[<System.Windows.Markup.ContentProperty("Items")>]
[<System.Windows.StyleTypedProperty(Property="ItemContainerStyle", StyleTargetType=typeof(System.Windows.FrameworkElement))>]
type ItemsControl = class
inherit Control
interface IAddChild
interface IContainItemStorage
[<System.Windows.Localizability(System.Windows.LocalizationCategory.None, Readability=System.Windows.Readability.Unreadable)>]
[<System.Windows.Markup.ContentProperty("Items")>]
[<System.Windows.StyleTypedProperty(Property="ItemContainerStyle", StyleTargetType=typeof(System.Windows.FrameworkElement))>]
type ItemsControl = class
inherit Control
interface IContainItemStorage
interface IAddChild
Public Class ItemsControl
Inherits Control
Implements IAddChild
Public Class ItemsControl
Inherits Control
Implements IAddChild, IContainItemStorage
ItemsControl
下列範例示範將資料系結至
ItemsControl
。 第一個範例會建立稱為
MyData
的類別,這是簡單的字串集合。
public class MyData : ObservableCollection<string>
public MyData()
Add("Item 1");
Add("Item 2");
Add("Item 3");
Public Class MyData
Inherits ObservableCollection(Of String)
Public Sub New() '
Add("Item 1")
Add("Item 2")
Add("Item 3")
End Sub
End Class
下列範例會將
ItemsSource
的
ItemsControl
物件系結至
MyData
。
<!--Create an instance of MyData as a resource.-->
<src:MyData x:Key="dataList"/>
<ListBox ItemsSource="{Binding Source={StaticResource dataList}}"/>
ListBox listBox1 = new ListBox();
MyData listData = new MyData();
Binding binding1 = new Binding();
binding1.Source = listData;
listBox1.SetBinding(ListBox.ItemsSourceProperty, binding1);
Dim listBox1 As New ListBox()
Dim listData As New MyData()
Dim binding1 As New Binding()
binding1.Source = listData
listBox1.SetBinding(ListBox.ItemsSourceProperty, binding1)
下圖顯示
ListBox
上一個範例中建立的控制項。
下列範例示範如何使用 屬性填入
ItemsControl
Items
。 此範例會將下列不同類型的專案新增至
ListBox
:
DateTime
物件。
UIElement
物件。
Panel
包含其他
UIElement
物件的控制項。
<!--Create a ListBox that contains a string, a Rectangle,
a Panel, and a DateTime object. These items can be accessed
via the Items property.-->
<ListBox xmlns:sys="clr-namespace:System;assembly=mscorlib"
Name="simpleListBox">
<!-- The <ListBox.Items> element is implicitly used.-->
This is a string in a ListBox
<sys:DateTime>2004/3/4 13:6:55</sys:DateTime>
<Rectangle Height="40" Width="40" Fill="Blue"/>
<StackPanel Name="itemToSelect">
<Ellipse Height="40" Fill="Blue"/>
<TextBlock>Text below an Ellipse</TextBlock>
</StackPanel>
<TextBlock>String in a TextBlock</TextBlock>
</ListBox>
// Add a String to the ListBox.
listBox1.Items.Add("This is a string in a ListBox");
// Add a DateTime object to a ListBox.
DateTime dateTime1 = new DateTime(2004, 3, 4, 13, 6, 55);
listBox1.Items.Add(dateTime1);
// Add a Rectangle to the ListBox.
Rectangle rect1 = new Rectangle();
rect1.Width = 40;
rect1.Height = 40;
rect1.Fill = Brushes.Blue;
listBox1.Items.Add(rect1);
// Add a panel that contains multpile objects to the ListBox.
Ellipse ellipse1 = new Ellipse();
TextBlock textBlock1 = new TextBlock();
ellipse1.Width = 40;
ellipse1.Height = 40;
ellipse1.Fill = Brushes.Blue;
textBlock1.TextAlignment = TextAlignment.Center;
textBlock1.Text = "Text below an Ellipse";
stackPanel1.Children.Add(ellipse1);
stackPanel1.Children.Add(textBlock1);
listBox1.Items.Add(stackPanel1);
' Create a Button with a string as its content.
listBox1.Items.Add("This is a string in a ListBox")
' Create a Button with a DateTime object as its content.
Dim dateTime1 As New DateTime(2004, 3, 4, 13, 6, 55)
listBox1.Items.Add(dateTime1)
' Create a Button with a single UIElement as its content.
Dim rect1 As New Rectangle()
rect1.Width = 40
rect1.Height = 40
rect1.Fill = Brushes.Blue
listBox1.Items.Add(rect1)
' Create a Button with a panel that contains multiple objects
' as its content.
Dim ellipse1 As New Ellipse()
Dim textBlock1 As New TextBlock()
ellipse1.Width = 40
ellipse1.Height = 40
ellipse1.Fill = Brushes.Blue
textBlock1.TextAlignment = TextAlignment.Center
textBlock1.Text = "Text below an Ellipse"
stackPanel1.Children.Add(ellipse1)
stackPanel1.Children.Add(textBlock1)
listBox1.Items.Add(stackPanel1)
下圖顯示
ListBox
上一個範例中建立的 。
下列範例說明如何使用 所提供的
ItemsControl
不同樣式和範本化相關屬性。
ItemsControl
此範例中的 系結至 物件的集合
Task
。 為方便示範,這個範例中的樣式和範本都是內嵌宣告的。
<ItemsControl Margin="10"
ItemsSource="{Binding Source={StaticResource myTodoList}}">
<!--The ItemsControl has no default visual appearance.
Use the Template property to specify a ControlTemplate to define
the appearance of an ItemsControl. The ItemsPresenter uses the specified
ItemsPanelTemplate (see below) to layout the items. If an
ItemsPanelTemplate is not specified, the default is used. (For ItemsControl,
the default is an ItemsPanelTemplate that specifies a StackPanel.-->
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl">
<Border BorderBrush="Aqua" BorderThickness="1" CornerRadius="15">
<ItemsPresenter/>
</Border>
</ControlTemplate>
</ItemsControl.Template>
<!--Use the ItemsPanel property to specify an ItemsPanelTemplate
that defines the panel that is used to hold the generated items.
In other words, use this property if you want to affect
how the items are laid out.-->
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<!--Use the ItemTemplate to set a DataTemplate to define
the visualization of the data objects. This DataTemplate
specifies that each data object appears with the Priority
and TaskName on top of a silver ellipse.-->
<ItemsControl.ItemTemplate>
<DataTemplate>
<DataTemplate.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="18"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
</DataTemplate.Resources>
<Ellipse Fill="Silver"/>
<StackPanel>
<TextBlock Margin="3,3,3,0"
Text="{Binding Path=Priority}"/>
<TextBlock Margin="3,0,3,7"
Text="{Binding Path=TaskName}"/>
</StackPanel>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
<!--Use the ItemContainerStyle property to specify the appearance
of the element that contains the data. This ItemContainerStyle
gives each item container a margin and a width. There is also
a trigger that sets a tooltip that shows the description of
the data object when the mouse hovers over the item container.-->
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Control.Width" Value="100"/>
<Setter Property="Control.Margin" Value="5"/>
<Style.Triggers>
<Trigger Property="Control.IsMouseOver" Value="True">
<Setter Property="Control.ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=Content.Description}"/>
</Trigger>
</Style.Triggers>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
下圖是轉譯時範例的螢幕擷取畫面。
此處未顯示的 其他兩個樣式相關屬性
ItemsControl
為
GroupStyle
和
GroupStyleSelector
。
ItemsControl
是 的型
Control
別,可以包含多個專案,例如字串、物件或其他專案。 下圖顯示
ListBox
控制項,其中包含下列不同類型的專案:
DateTime
物件。
UIElement
物件。
Panel
包含
Ellipse
和 的
TextBlock
控制項。
包含多種物件類型的 ListBox
Items
使用 或
ItemsSource
屬性來指定要用來產生 內容的
ItemsControl
集合。 您可以將 屬性設定
ItemsSource
為任何實作
IEnumerable
的類型。
ItemsSource
通常用來顯示資料收集,或系結
ItemsControl
至集合物件。
如果您不想使用實作
IEnumerable
的物件來填入
ItemsControl
,您可以使用 屬性來新增專案
Items
。 中的
ItemsControl
專案可以有不同的類型。 例如,
ListBox
可以包含一個專案是字串,另一個專案是
Image
。
ItemsSource
設定屬性時,
Items
集合會設定為唯讀且固定大小。 這表示您無法直接將專案新增至集合。 使用 時
ItemsSource
,將 屬性設定為
null
移除集合,並將使用量還原為
Items
,這會是空
ItemCollection
的 。
每個
ItemsControl
類型都有對應的專案容器類型。 每個
ItemsControl
的對應專案容器會附加
Item
至其名稱。 例如,針對
ListBox
,專案容器是
ListBoxItem
控制項;如果是
ComboBox
,則為
ComboBoxItem
控制項。 您可以針對 中的每個
ItemsControl
專案明確建立容器類型,但並非必要專案。 當您未明確建立容器類型時,會產生一個包含專案集合中的資料項目。 例如,如果您將字串物件的集合系結至
ItemsSource
的
ListBox
屬性,則不會明確建立
ListBoxItem
物件,但
ListBox
會為每個字串產生一個。 您可以使用 屬性來存取產生的專案容器
ItemContainerGenerator
。
當 包含重複的物件時
ItemsControl
,UI 自動化的某些功能無法正常運作。 如果物件出現多次,則只有第一個實例會出現在自動化樹狀結構中。 (如果
Object.Equals(x, y)
傳回 .) ,則會將兩個物件
x
和
y
視為重複專案
true
當物件
x
正由
ItemsControl
x 傳回的值使用時
。
GetHashCode()
不得變更。 不支援對此值所做的變更,並導致無法預期的行為。
此控制項的相依性屬性可能是由控制項的預設樣式所設定。 如果屬性是以預設樣式設定,當控制項出現在應用程式中時,屬性可能會從其預設值變更。 預設樣式取決於應用程式執行時所使用的桌面主題。
加入所指定路由事件的路由事件處理常式,會將此處理常式加入目前項目的處理常式集合中。 將
handledEventsToo
指定為
true
,為已標示為由事件路由上另一個項目處理的事件叫用提供的處理常式。
(繼承來源
UIElement
)
放置子項目,並且判斷
UIElement
的大小。 父項目從
ArrangeCore(Rect)
實作 (或 WPF 架構層級對等項目) 中呼叫此方法,以形成遞迴配置更新。 這個方法會構成配置更新的第二個階段。
(繼承來源
UIElement
)
強制轉型所指定相依性屬性的值。 完成方式是叫用存在於呼叫
DependencyObject
之相依性屬性的屬性中繼資料中所指定的任何
CoerceValueCallback
函式。
(繼承來源
DependencyObject
)
更新
UIElement
的
DesiredSize
。 父項目從本身的
MeasureCore(Size)
實作中呼叫此方法,以形成遞迴配置更新。 呼叫此方法即構成配置更新的第一次傳遞 (「量值」傳遞)。
(繼承來源
UIElement
)
每當這個
FrameworkElement
上任何相依性屬性的有效值已更新時叫用。 已變更的特定相依性屬性會在引數參數中報告。 覆寫
OnPropertyChanged(DependencyPropertyChangedEventArgs)
。
(繼承來源
FrameworkElement
)
在
FrameworkElement
的特定子類別中支援累加配置實作。
ParentLayoutInvalidated(UIElement)
會在子項目使得屬性失效時叫用,該屬性在中繼資料內已標記,會在配置期間影響父代的測量或排列傳遞。
(繼承來源
FrameworkElement
)