Is there any native WPF Multiselect combobox available?

前端 未结 7 2045
予麋鹿
予麋鹿 2020-12-17 17:48

Even a 3rd party one will do.

Thanks

相关标签:
7条回答
  • 2020-12-17 18:17

    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" />
    
    0 讨论(0)
  • 2020-12-17 18:18

    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
    
    0 讨论(0)
  • 2020-12-17 18:21

    Although I am yet to get this to work, this looks like what I need and similar to what you are looking for:Just Guy's Blog

    0 讨论(0)
  • 2020-12-17 18:22

    I found this useful information from Codeproject - ComboBoxMultiSelect

    I haven't tried it myself as of yet, but would let know about my experience.

    0 讨论(0)
  • 2020-12-17 18:25

    I used an expander and filled the expander's header with the selection and the content with a list box. The list box is binded to a collection. Whenever user make a selection, I update the header to show what user has selected.

    0 讨论(0)
  • 2020-12-17 18:35

    There is no native multiselect combobox in WPF. Please check my blog for a simple hack using expression blend to achieve a multi selection on combobox. http://jobijoy.blogspot.com/2009/02/simple-multiselect-combobox-using.html The idea is to utilize the Multi-Selection feature of ListBox in to ComboBox by editing the control template.

    But for accessing the selected items you might need to use the bellow line in the code.

    ((ListBox)cmbBox.Template.FindName("lstBox",cmbBox)).SelectedItems
    

    Where cmbBox is your combobox and lstBox is the ListBox inside the controltemaplate.

    0 讨论(0)
提交回复
热议问题