要修改WPF中的Items元素,您需要進(jìn)行以下步驟:
<ListBox.Items>
標(biāo)記訪問。<ListBox.ItemsPanel>
元素來指定Items的布局面板。<ListBox.ItemTemplate>
元素來定義每個(gè)Items元素的外觀。在ItemTemplate中,您可以使用數(shù)據(jù)綁定和其他控件來自定義每個(gè)Items元素的顯示。<ListBox.ItemContainerStyle>
元素來指定每個(gè)Items元素的樣式。以下是一個(gè)ListBox的示例,演示如何修改Items元素:
<ListBox>
<ListBox.Items>
<ListBoxItem>Item 1</ListBoxItem>
<ListBoxItem>Item 2</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
</ListBox.Items>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Background" Value="LightBlue"/>
<Setter Property="Margin" Value="5"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
在這個(gè)示例中,我們將ItemsPanel設(shè)置為一個(gè)水平方向的StackPanel,ItemTemplate設(shè)置為一個(gè)包含TextBlock的DataTemplate,ItemContainerStyle設(shè)置為具有淺藍(lán)色背景和5像素邊距的樣式。