【问题标题】:C# WPF Combobox dropdown list to multiple columnsC# WPF Combobox 下拉列表到多列
【发布时间】:2013-02-04 13:07:02
【问题描述】:

是否可以“强制”列表中的组合框项目出现在两列中?

例如这样:

CB 选择项目

CB 项目 1 | CB 项目 4

CB 项目 2 | CB 项目 5

CB 项目 3 |

【问题讨论】:

  • 有什么特别的原因让你希望它先垂直然后水平?
  • @UğurGümüşhan 我们不知道什么对 Kristo更多 更重要,如果你要责怪某人关心 UI(或好奇),那么我'我不和你在一起。
  • @UğurGümüşhan 我在考虑功能方面的问题,如果他有 100 个条目,要达到第 50 个,客户必须滚动到 ComboBox 列表的底部(因为它们会被订购一半(垂直)而不是二乘二),而不是中途。当他有数千人时会发生什么?
  • 如果他有 100 或 1000 个条目,他最好还是不要使用 Combo :)
  • html 没有多列组合框是有原因的。

标签: c# wpf list combobox multiple-columns


【解决方案1】:

你可以把ItemsPanel改成WrapPanel,注意高度(你可以写一个转换器来根据项目的数量计算它):

<ComboBox>
    <ComboBox.Resources>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate>
                        <WrapPanel IsItemsHost="True" Orientation="Vertical" Width="100" Height="50" />
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style TargetType="ComboBoxItem">
            <Setter Property="Width" Value="50" />
        </Style>
    </ComboBox.Resources>

    <ComboBoxItem Content="Value 1" />
    <ComboBoxItem Content="Value 2" />
    <ComboBoxItem Content="Value 3" />
    <ComboBoxItem Content="Value 4" />
    <ComboBoxItem Content="Value 5" />
</ComboBox>

【讨论】:

  • 如果您将ItemTemplate 用于ComboBox 并使用ItemsSource 而不是单独输入ComboBoxItem,这是否也有效?
  • 是的,它确实适用于 DataTemplateItemsSource ...非常感谢 @mathieu :)
【解决方案2】:

好吧,你可以,这是 XAML:

<ComboBox Name="ComboBox">
    <ComboBox.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="2"/>
        </ItemsPanelTemplate>
    </ComboBox.ItemsPanel>
</ComboBox>

现在做一个简单的测试,将 0 到 8 的数字相加:

现在你可以随心所欲地设计它... :)

当然,每个项目(在这种特殊情况下是每个数字)都是独立的、可点击的项目,这样就不会产生误解。

[编辑] 我刚刚注意到您想以“相反的方式”进行操作,即在“行”方向,如果是这样,那么也许最好使用 WrapPanel 代替,正如其他人所建议的那样回答。 UniformGrid 首先按列方向填充网格。

也许有一种方法可以使用UniformGrid,但没有明显且简单的一键更改(我之前在这里错了:))

【讨论】:

  • 您将其表示为与原始矩阵相比的转置矩阵,这并没有给出操作想要的内容
  • @UğurGümüşhan 是的,我注意到了,因此是编辑部分。 :)
  • 无论如何感谢您的输入,我以后可能会使用 UniformGrid ;)
【解决方案3】:

您需要将WrapPanel 放入组合框的ItemsPanel

<ComboBox>
    <ComboBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Vertical" Height="100" />
        </ItemsPanelTemplate>
    </ComboBox.ItemsPanel>

    <ComboBoxItem Content="Value 1" />
    <ComboBoxItem Content="Value 2" />
    <ComboBoxItem Content="Value 3" />
    <ComboBoxItem Content="Value 4" />
    <ComboBoxItem Content="Value 5" />
    <ComboBoxItem Content="Value 6" />
    <ComboBoxItem Content="Value 7" />
    <ComboBoxItem Content="Value 8" />
    <ComboBoxItem Content="Value 9" />
    <ComboBoxItem Content="Value 10" />
    <ComboBoxItem Content="Value 11" />
    <ComboBoxItem Content="Value 12" />
    <ComboBoxItem Content="Value 13" />
    <ComboBoxItem Content="Value 14" />
    <ComboBoxItem Content="Value 15" />
</ComboBox>

【讨论】:

    【解决方案4】:

    您可以尝试将控件模板更改为使用 Grid 并使用转换器来决定 cbitems 是哪一列和哪一行。不过,我不确定您将如何处理所选项目。

    【讨论】:

    • 这是关于流布局的。不是列。
    • 是的,我明白你的意思。看着人们提供的例子,我意识到他/她的要求。
    猜你喜欢
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    • 2011-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多