WPFでItems要素を編集する方法
WPF で Items 要素を変更するには、次の手順を実行する必要があります。
- ListBoxやComboBoxなど、Items要素を含むコントロールを見つけます。
- リストボックス.リスト
- リストボックスのアイテムのテンプレート
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>
このサンプルでは、ItemsPanelを水平方向のStackPanelに設定し、ItemTemplateをTextBlockを含むDataTemplateに設定し、ItemContainerStyleを水色背景と5ピクセルのマージンを持つスタイルに設定します。