是否有可用的原生WPF Multiselect组合框?

时间:2009-02-03 19:22:15

标签: wpf combobox wpf-controls multi-select

即使是第三方也会这样做。

由于

7 个答案:

答案 0 :(得分:13)

我不确定ComboBox如何以这种方式显示数据,因为它被设计为单选控件。

也许您正在寻找像ListBoxListView这样SelectionMode MultipleExtended的内容?

<ListBox SelectionMode="Multiple" />

<ListBox SelectionMode="Extended" />

答案 1 :(得分:8)

WPF中没有原生的多选组合框。请查看我的博客,了解使用表达式混合的简单黑客,以实现对组合框的多选。 http://jobijoy.blogspot.com/2009/02/simple-multiselect-combobox-using.html 我们的想法是通过编辑控件模板,将ListBox的Multi-Selection功能用于ComboBox。

但是,要访问所选项目,您可能需要使用代码中的波纹管线。

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

cmbBox 是你的组合框, lstBox 是控制板内的ListBox。

答案 2 :(得分:3)

我使用了一个扩展器,并在扩展器的标题中填充了选择内容和带有列表框的内容。列表框绑定到集合。每当用户做出选择时,我都会更新标题以显示用户选择的内容。

答案 3 :(得分:3)

我在Codeproject中找到了这个有用的信息 - ComboBoxMultiSelect

到目前为止我还没有尝试过,但是会让我知道我的经历。

答案 4 :(得分:1)

虽然我还没有让它发挥作用,但这看起来像我需要的东西,与您所寻找的类似:Just Guy's Blog

答案 5 :(得分:0)

另一个CodeProject详细解释了如何创建具有多个可选复选框的ComboBox: Multi Select ComboBox in WPF

答案 6 :(得分:0)

万一它对任何人都有用,我已经做了一个粗糙而现成的多选ComboBox。 基本上只是一个带有按钮,列表框和弹出窗口的TextBlock。我认为容易建立。 设置为使用选择作为字符串列表(列表),itemsSource作为列表字符串(列表),并引发selectionsChanges事件。

XAML :(不包括设计尺寸的用户控件)

<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>

和代码。

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