次の方法で共有


ComboBox.ItemContainerStyle Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets the style applied to the container generated for each item in the combo box.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Property ItemContainerStyle As Style
public Style ItemContainerStyle { get; set; }
<ComboBox>
  <ComboBox.ItemContainerStyle>
    inlineStyle
  </ComboBox.ItemContainerStyle>
</ComboBox>
<ComboBox ItemContainerStyle="styleReference"/>

XAML Values

For information that can help you decide whether to define styles inline or as resources, see Inline Styles and Templates.

Property Value

Type: System.Windows.Style
The style applied to the container generated for each item in the combo box.

Remarks

Dependency property identifier field: ItemContainerStyleProperty

You can use the ItemContainerStyle to provide a custom appearance for combo box items listed in the drop-down portion. This style does not apply to the selected item displayed in the text box.

Examples

The following example demonstrates how to set the ItemContainerStyle property to display a custom style for a list of bound combo box items.

Partial Public Class Page
    Inherits UserControl
    Public MyMusic As New ObservableCollection(Of Recording)()
    Public Sub New()
        InitializeComponent()
        MyMusic.Add(New Recording("Sheryl Crow", "Detours", New DateTime(2008, 2, 5)))
        MyMusic.Add(New Recording("Brandi Carlisle", "The Story", New DateTime(2007, 4, 3)))
        MyMusic.Add(New Recording("Patty Griffin", "Children Running Through", New DateTime(2007, 2, 6)))
        MusicCombo.DataContext = MyMusic
    End Sub
End Class

Public Class Recording
    Public Sub New()
    End Sub
    Public Sub New(ByVal artistName As String, ByVal cdName As String, ByVal release As DateTime)
        Artist = artistName
        Name = cdName
        ReleaseDate = release
    End Sub
    Private artistValue As String
    Private nameValue As String
    Private releaseDateValue As DateTime
    Private dateTi
    Public Property Artist() As String
        Get
            Return artistValue
        End Get
        Set(ByVal value As String)
            artistValue = value
        End Set
    End Property
    Public Property Name() As String
        Get
            Return nameValue
        End Get
        Set(ByVal value As String)
            nameValue = value
        End Set
    End Property
    Public Property ReleaseDate() As DateTime
        Get
            Return releaseDateValue
        End Get
        Set(ByVal value As DateTime)
            releaseDateValue = value
        End Set
    End Property
End Class
public partial class Page : UserControl
{
    public ObservableCollection<Recording> MyMusic = new ObservableCollection<Recording>();
    public Page()
    {
        InitializeComponent();
        MyMusic.Add(new Recording("Sheryl Crow", "Detours", new DateTime(2008, 2, 5)));
        MyMusic.Add(new Recording("Brandi Carlisle", "The Story", new DateTime(2007, 4,3)));
        MyMusic.Add(new Recording("Patty Griffin", "Children Running Through", new DateTime( 2007, 2, 6)));
        MusicCombo.DataContext = MyMusic;
    }
}

public class Recording
{
    public Recording(){}
    public Recording (string artistName, string cdName, DateTime release)
    {
        Artist = artistName;
        Name = cdName;
        ReleaseDate = release;
    }
    public string Artist { get; set; }
    public string Name { get; set; }
    public DateTime ReleaseDate {get; set;}
}
<Grid.Resources>
    <Style TargetType="ComboBoxItem" x:Key="musicStyle" >
        <Setter Property="Foreground" Value="Blue" />
        <Setter Property="FontStyle" Value="Italic" />
    </Style>
</Grid.Resources>

<ComboBox Height="40" Width="200" x:Name="MusicCombo" 
    ItemContainerStyle="{StaticResource musicStyle}" ItemsSource="{Binding}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" />
                <TextBlock Text="{Binding Path=Artist}" />
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.