Phong Le
Phong Le

Reputation: 281

Is there a simple way to put a datagrid into a combobox?

Is there a simple example of how to have a combobox show a datagrid instead of a list? I been trying SO and google but kept getting results that show how to put a combobox in a datagrid.

Upvotes: 0

Views: 119

Answers (3)

ThomasAndersson
ThomasAndersson

Reputation: 1904

To answer your first question, "Is there a simple example of how to have a combobox show a datagrid instead of a list?" very straight: - the answer is no.

That said, there are a couple of different ways to accomplish something that looks like a DataGrid (ie list with columns).

A very simple sample with faking something that could look like a datagrid could be altering the combobox's ItemTemplate

<ComboBox x:Name="cb" ItemsSource="{Binding Persons}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="50" />
                    <ColumnDefinition Width="50" />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{Binding Name}" />
                <TextBlock Grid.Column="1" Text="{Binding Age}" />
            </Grid>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

You can also use SharedSizeGroup (and Grid.IsSharedSizeScope) to synchronize the column widths accross all items resulting in a dynamic and aligned layout.

Upvotes: 2

ppiotrowicz
ppiotrowicz

Reputation: 4614

As I said, it's weird to me to use a ComboBox for this. Maybe you can do it like this:

 <ToggleButton x:Name="Toggle" Width="200" Height="30" HorizontalAlignment="Center" VerticalAlignment="Top" />
    <Popup IsOpen="{Binding ElementName=Toggle, Path=IsChecked}" PlacementTarget="{Binding ElementName=Toggle}" Placement="Bottom" Width="{Binding ElementName=Toggle, Path=Width}">
        <DataGrid>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Test1" />
                <DataGridTextColumn Header="Test2" />
            </DataGrid.Columns>
        </DataGrid>
    </Popup>

It's only a example of course, and it's missing a few features like automatic Popup hiding. But you get the point.

Upvotes: 0

Tigran
Tigran

Reputation: 62265

It's for sure possible. That is a power of WPF.

This link explains how to do it.

Upvotes: 1

Related Questions