Is there any native WPF Multiselect combobox available?

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

Even a 3rd party one will do.

Thanks

7条回答
  •  有刺的猬
    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)

    
        
            
            
        
        
            
        
        
        
            
        
        
        
            
        
    
    

    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
    

提交回复
热议问题