Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any native WPF Multiselect combobox available?

Even a 3rd party one will do.

Thanks

like image 595
Amit Sharma Avatar asked Feb 03 '09 19:02

Amit Sharma


2 Answers

I'm not sure how a ComboBox would display data in this fashion, as it is designed as a single-selection Control.

Maybe you are looking for something like a ListBox or ListView with a SelectionMode of Multiple or Extended?

<ListBox SelectionMode="Multiple" />

<ListBox SelectionMode="Extended" />
like image 72
Adrian Avatar answered Sep 22 '22 23:09

Adrian


In case it is useful to anyone, I've made a rough and ready multi-select ComboBox. Basically just a TextBlock with a Button, ListBox and a Popup. Easy to build upon I think. Set to work with selections as list(of String), itemsSource as a list(of String), and raises a selectionsChanges event.

XAML: (user control with design dimensions excluded)

<Grid Margin="0,4,0,0">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="18"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Border x:Name="bdr" BorderBrush="Gray" BorderThickness="1" Grid.ColumnSpan="2"/>
    <TextBlock x:Name="txtValues" MinWidth="50" Background="#F0F0F0" Margin="1,1,0,1" Padding="3,1,0,1">
        <TextBlock.ContextMenu><ContextMenu><MenuItem Header="Clear" Click="clearItems"/></ContextMenu></TextBlock.ContextMenu>
    </TextBlock>
    <Button Grid.Column="1" Click="showListBox">
        <Polygon Points="0,2 12,2 6,8" Fill="Black"/>
    </Button>
    <Popup x:Name="pop" StaysOpen="False" Grid.ColumnSpan="2"
           PlacementTarget="{Binding ElementName=bdr}" Closed="pop_Closed">
        <ListBox x:Name="items" SelectionMode="Extended"
                 Width="{Binding ElementName=bdr,Path=ActualWidth}"/>
    </Popup>
</Grid>

and code..

Public Class multiCombo
Private _itemsSource As List(Of String)
Private _selections As List(Of String)
Public Event selectionsChanges(sender As Object, e As EventArgs)
Public Property selections As List(Of String)
    Get
        Return _selections
    End Get
    Set
        _selections = Value
        For Each itm In items.Items
            If Value.Contains(itm) Then
                If Not items.SelectedItems.Contains(itm) Then items.SelectedItems.Add(itm)
            Else
                If items.SelectedItems.Contains(itm) Then items.SelectedItems.Remove(itm)
            End If
        Next
        txtValues.Text = String.Empty
        For Each itm In Value
            If txtValues.Text.Length > 0 Then txtValues.Text += ", "
            txtValues.Text += itm
        Next
    End Set
End Property
Public Property itemsSource As List(Of String)
    Get
        Return _itemsSource
    End Get
    Set
        _itemsSource = Value
        items.ItemsSource = Value
    End Set
End Property
Private Sub showListBox(sender As Object, e As RoutedEventArgs)
    pop.IsOpen = True
End Sub
Private Sub pop_Closed(sender As Object, e As EventArgs)
    Dim changed = items.SelectedItems.Count <> selections.Count
    If Not changed Then
        For Each selItm In items.SelectedItems
            If Not selections.Contains(selItm) Then changed = True
        Next
    End If
    If changed Then
        selections.Clear()
        txtValues.Text = String.Empty
        For Each selItm In items.SelectedItems
            selections.Add(selItm)
            If txtValues.Text.Length > 0 Then txtValues.Text += ", "
            txtValues.Text += selItm
        Next
        RaiseEvent selectionsChanges(Me, Nothing)
    End If
End Sub
Private Sub clearItems(sender As Object, e As RoutedEventArgs)
    If selections.Count > 0 Then
        selections.Clear()
        txtValues.Text = String.Empty
        items.SelectedItems.Clear()
        RaiseEvent selectionsChanges(Me, Nothing)
    End If
End Sub
End Class
like image 42
Mikey Avatar answered Sep 23 '22 23:09

Mikey