如何在多列中生成下拉组合框?

时间:2016-05-03 09:36:19

标签: wpf visual-studio-2015

我已经搜索过这个主题,但只发现需要用一个comboBoxItem填充各种列。 我需要的是一个列的列表,在各列中变形以缩短comboBox的总长度,另外设置一些标题来分隔部分以便更容易搜索一个项目。 这是我正在寻找的一个例子:

multi columns ComboBox

我在ComboBox样式中弄乱了Popup标记内的grid.columns定义,但是我没有得到正确的结果。

感谢。

1 个答案:

答案 0 :(得分:0)

XAML:

    <ComboBox Height="23" HorizontalAlignment="Left" Margin="84,51,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Text}" />
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
        <ComboBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Vertical" Height="100" />
            </ItemsPanelTemplate>
        </ComboBox.ItemsPanel>
        <ComboBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="IsEnabled" Value="{Binding CanSelect}" />
            </Style>
        </ComboBox.ItemContainerStyle>
    </ComboBox>

C#:

    public class Item
    {
        public string Text { get; set; }
        public bool CanSelect { get; set; }
    }

    public class SelectableItem : Item
    {
        public SelectableItem()
        {
            CanSelect = true;
        }
    }

    public class Header : Item
    {
    }

    // inside constructor or wherever:
    var items = new Item[] {
        new Header() { Text = "Header1"},
        new SelectableItem() { Text = "ComboboxItem"},
        new SelectableItem() { Text = "ComboboxItem"},
        new SelectableItem() { Text = "ComboboxItem"},
        new SelectableItem() { Text = "ComboboxItem"},
        new SelectableItem() { Text = "ComboboxItem"},
        new Header() { Text = "Header2"},
        new SelectableItem() { Text = "ComboboxItem"},
        new SelectableItem() { Text = "ComboboxItem"},
        new Header() { Text = "Header3"},
        new SelectableItem() { Text = "ComboboxItem"},
        new SelectableItem() { Text = "ComboboxItem"},
        new SelectableItem() { Text = "ComboboxItem"},
        new SelectableItem() { Text = "ComboboxItem"},
        new SelectableItem() { Text = "ComboboxItem"},
        new SelectableItem() { Text = "ComboboxItem"}
    };
    comboBox1.ItemsSource = items;
相关问题