C# WPF 组合框下拉列表到多列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14687422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:22:17  来源:igfitidea点击:

C# WPF Combobox dropdown list to multiple columns

c#wpflistcomboboxmultiple-columns

提问by Kristo

Is it possible to "force" the combobox items in list to appear in say two columns?

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

For example like this:

例如像这样:

CB Selected Item

CB 选定项目

CB Item 1 | CB Item 4

CB 项目 1 | CB 第 4 项

CB Item 2 | CB Item 5

CB 项目 2 | CB 第 5 项

CB Item 3 |

CB 项目 3 |

回答by Patryk ?wiek

Well, you can, here's the XAML:

好吧,你可以,这是 XAML:

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

Now a simple test, adding numbers from 0 to 8 gives:

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

uniform grid in combo box

组合框中的统一网格

Now you can style it all you want... :)

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

Of course every item (every number, in this particular case) is separate, clickable item, just so there are no misunderstandings.

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

[EDIT] I have just noticed you want to do it 'the opposite way', that is in 'rows' direction, if so, then maybe it's better to use the WrapPanelinstead, as someone suggested in the other answer. The UniformGridfills the grid in the column-wise direction first.

[编辑] 我刚刚注意到你想以“相反的方式”来做,那就是在“行”的方向上,如果是这样,那么也许最好使用WrapPanel代替,正如有人在另一个答案中所建议的那样。首先UniformGrid按列方向填充网格。

Maybe there's a way to do it with UniformGrid, but there's no apparent and easy one-click change (I was wrong here before :) )

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

回答by mathieu

You can change the ItemsPanel to a WrapPanel, just be careful on the height (you could write a converter to calculate it according to the number of items) :

您可以将 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>

回答by Michael Schnerring

You would need to put a WrapPanelinto the ItemsPanelof the combobox.

您需要将 aWrapPanel放入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>

enter image description here

在此处输入图片说明

回答by TYY

you could try changing the control template to use Grid and use converters to decide what column and row the cbitems are. I am not sure how you'd handle the selected item though.

您可以尝试更改控件模板以使用 Grid 并使用转换器来决定 cbitems 是什么列和行。我不确定你会如何处理所选项目。