是否有可用的本机 WPF 多选组合框?

发布于 2024-07-13 07:25:57 字数 32 浏览 4 评论 0原文

即使是第 3 方也可以。

谢谢

Even a 3rd party one will do.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

养猫人 2024-07-20 07:25:57

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

也许您正在寻找类似 ListBoxListView 的内容,其 SelectionModeMultipleExtend< /代码>?

<ListBox SelectionMode="Multiple" />

<ListBox SelectionMode="Extended" />

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" />
云仙小弟 2024-07-20 07:25:57

WPF 中没有本机多选组合框。 请查看我的博客,了解使用表达式混合实现组合框多重选择的简单技巧。
http://jobijoy.blogspot.com/2009/02/simple -multiselect-combobox-using.html
其想法是通过编辑控件模板将 ListBox 的多选功能应用到 ComboBox 中。

但要访问所选项目,您可能需要在代码中使用以下行。

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

其中 cmbBox 是您的组合框,lstBox 是控件模板内的列表框。

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.

墨洒年华 2024-07-20 07:25:57

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

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.

一片旧的回忆 2024-07-20 07:25:57

我从 Codeproject - ComboBoxMultiSelect

我自己还没有尝试过,但会告诉我我的经验。

I found this useful information from Codeproject - ComboBoxMultiSelect

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

往事风中埋 2024-07-20 07:25:57

如果它对任何人都有用,我已经制作了一个粗略且现成的多选组合框。
基本上只是一个带有按钮、列表框和弹出窗口的文本块。 我认为很容易建立。
设置为将选择作为列表(字符串)、itemsSource 作为列表(字符串),并引发选择更改事件。

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

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
夜唯美灬不弃 2024-07-20 07:25:57

我知道很久以前就有人问过这个问题,但遇到这个问题并认为我会放弃更新的答案。

您可以将 ComboBox 与复选框模板一起使用。 例如:

<ComboBox ItemsSource="{Binding Items}">
  <ComboBox.ItemTemplate>
    <DataTemplate>
      <CheckBox Content="{Binding Name}"></CheckBox>
    </DataTemplate>
  </ComboBox.ItemTemplate>
</ComboBox>

希望这有帮助

I know this was asked a long time ago but came across this and figured I'd drop a more up-to-date answers.

You can use a ComboBox with checkbox templates. For Example:

<ComboBox ItemsSource="{Binding Items}">
  <ComboBox.ItemTemplate>
    <DataTemplate>
      <CheckBox Content="{Binding Name}"></CheckBox>
    </DataTemplate>
  </ComboBox.ItemTemplate>
</ComboBox>

Hope this helps

作妖 2024-07-20 07:25:57

另一个 CodeProject 详细说明了如何创建具有多个可选复选框的组合框:
WPF 中的多选组合框

Another CodeProject with detailed explanations how to create a ComboBox with multiple selectable Checkboxes:
Multi Select ComboBox in WPF

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文