Com o WPF é possível criar vários tipos de ComboBox:
Simples, só com os “normais” dados
<ComboBox> <ComboBoxItem IsSelected="True">Valor 1</ComboBoxItem> <ComboBoxItem>Valor 2</ComboBoxItem> <ComboBoxItem>Valor 3</ComboBoxItem> </ComboBox>
Ou mais complexo, por exemplo com a adição de uma coluna onde se coloca uma imagem
<ComboBox> <ComboBoxItem IsSelected="True"> <StackPanel Orientation="Horizontal"> <Image Source="Imagens/Vermelho.png" /> <TextBlock Foreground="Red">Vermelho</TextBlock> </StackPanel> </ComboBoxItem> <ComboBoxItem> <StackPanel Orientation="Horizontal"> <Image Source="Imagens/Verde.png" /> <TextBlock Foreground="Green">Verde</TextBlock> </StackPanel> </ComboBoxItem> <ComboBoxItem> <StackPanel Orientation="Horizontal"> <Image Source="Imagens/Azul.png" /> <TextBlock Foreground="Blue">Azul</TextBlock> </StackPanel> </ComboBoxItem> </ComboBox>
Tendo por base o ultimo exemplo mas usando o DataBinding, tanto para mostrar como para atribuir o valor escolhido à propriedade que vai guardar a escolha.
Xaml
<StackPanel> <TextBlock IsEnabled="False" Text="{Binding CorEscolhida}" Margin="5"/> <ComboBox Name="cbCores" Margin="5,0" VerticalAlignment="Top" ItemsSource="{Binding Cores}" SelectedValue="{Binding CorEscolhida}" SelectedValuePath="Nome"> <ComboBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Image Source="{Binding Cor}" Margin="0,0,10,0"/> <TextBlock Text="{Binding Nome}" /> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> </StackPanel>
Xaml.vb
Public Class WPF_Controlos_ComboBox Public Sub New() InitializeComponent() Dim _ViewModel As New ViewModel With _ViewModel .Cores.Add(New Cor("/Imagens/BolaVermelho.png", "Vermelho")) .Cores.Add(New Cor("/Imagens/BolaVerde.png", "Verde")) .Cores.Add(New Cor("/Imagens/BolaAzul.png", "Azul")) End With Me.DataContext = _ViewModel cbCores.SelectedIndex = 0 End Sub End Class
ViewModel.vb
Public Class ViewModel Property Cores As New List(Of Cor) Property CorEscolhida As String End Class
Classe de apoio
Public Class Cor Property Cor As String Property Nome As String Public Sub New(_Cor As String, _Nome As String) Cor = _Cor Nome = _Nome End Sub End Class