Flash
Flash

Reputation: 115

WPF ComboBox that does not influence column width

How can I tell a ComboBox to not influence the column width of the grid that it is in?

Here is a minimal example:

<StackPanel Orientation="Vertical">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="5"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Label>This is some text</Label>
        <Label Grid.Row="1">This is some text</Label>
        <GridSplitter Grid.Column="1" Grid.RowSpan="2" HorizontalAlignment="Stretch"/>
        <Label Grid.Column="2" Background="Beige" HorizontalAlignment="Right">This is some text</Label>
        <ComboBox Grid.Row="1" Grid.Column="2">
            <ComboBoxItem IsSelected="True">This is some text</ComboBoxItem>
            <ComboBoxItem>This is some really lengthy text that is really long</ComboBoxItem>
        </ComboBox>
    </Grid>
</StackPanel>

The ComboBox changes its size when the second item is selected, and together with it, the size of the third column is changed (as can be seen by the beige background of the label). enter image description here

This also has the effect of the text in the beige label sometimes being outside of the visible area even though there is enough space to display it completely:

enter image description here

What I would want is that the third column always has the width of beige label (or any other element that is in the column and is not a ComboBox), and the ComboBox shortens its text so that it fits that width. The popup part of the ComboBox can be larger, I'm just talking about the button part here. I have tried setting up a ComboBox with TextBlock Content and TextTrimming set to CharacterEllipsis, but to no avail.

Upvotes: 2

Views: 1258

Answers (4)

Rudra
Rudra

Reputation: 144

Below is code for your reference

<Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="4*"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <ComboBox HorizontalAlignment="Stretch" Grid.Column="1">

Upvotes: -1

Nawed Nabi Zada
Nawed Nabi Zada

Reputation: 2875

Here this should do it for you:

<StackPanel Orientation="Vertical">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="5"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Label>This is some text</Label>
        <Label Grid.Row="1">This is some text</Label>
        <GridSplitter Grid.Column="1" Grid.RowSpan="2" HorizontalAlignment="Stretch"/>
        <Label x:Name="Label" Grid.Column="2" Background="Beige" HorizontalAlignment="Right">This is some text</Label>
        <ComboBox Grid.Row="1" Grid.Column="2" Width="{Binding ElementName=Label, Path=ActualWidth, Mode=OneWay}">
            <ComboBoxItem IsSelected="True">This is some text</ComboBoxItem>
            <ComboBoxItem>This is some really lengthy text that is really long</ComboBoxItem>
        </ComboBox>
    </Grid>
</StackPanel>

Upvotes: 2

E.Turkmen
E.Turkmen

Reputation: 1

You can use the Width property of Combobox like

<ComboBox Grid.Row="1" Grid.Column="2" Width="200">

Also text wrapping may help you, following link has an example to how you can use text wrapping for combobox: ComboBox TextWrap Binding

Upvotes: -1

Rudra
Rudra

Reputation: 144

Set MaxWidth of combobox control. It will allow combo box to expend to that value. If you want a fixed width then you need to set width property of combo box.

Upvotes: -1

Related Questions