2

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.

Flash
  • 107
  • 8
  • Set the `Width` property of the `ComboBox`, or set the `Width` of the `ColumnDefinition` to a fix value – enapi Dec 20 '16 at 14:01
  • But that is not what I want. I put the GridSplitter there, because it is in my original example. The requirement is that the ComboBox is as wide as all the other elements in the column. – Flash Dec 20 '16 at 14:22
  • What other elements ? – Nawed Nabi Zada Dec 20 '16 at 14:25
  • Well, in this case just the label, but obviously this is just a minimal example, and in reality there can be other elements in the column. – Flash Dec 20 '16 at 14:29

4 Answers4

2

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>
Nawed Nabi Zada
  • 2,819
  • 5
  • 29
  • 40
  • This works in the example, thank you. As in my real application the entire column is created at run-time, it is not that easy, but I guess I have to write code-behind that picks a random non-ComboBox control in that column and binds to that. – Flash Dec 20 '16 at 14:47
  • Just to give a conclusion, I just implemented the code-behind I mentioned above in my real code, and it works as required, so thank you again. – Flash Dec 20 '16 at 15:26
  • @Flash No problem, glad it helped. – Nawed Nabi Zada Dec 21 '16 at 07:17
0

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.

Rudra
  • 148
  • 1
  • 10
  • That is not what I want. The ComboBox should be as wide as all the other elements in the column and not some fixed value. Which elements are in the column (not in my minimal example) is decided at run-time. – Flash Dec 20 '16 at 14:23
  • Then you do the following set the width of column with * and set combo box HorizontalAlignment="Stretch". – Rudra Dec 20 '16 at 14:38
0

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

Community
  • 1
  • 1
E.Turkmen
  • 1
  • 1
  • Again, that is not what I want. The requirement is that the ComboBox is exactly as wide as the column but does not influence the size of the column. No fixed widths are allowed. – Flash Dec 20 '16 at 14:27
0

Below is code for your reference

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

        <ComboBox HorizontalAlignment="Stretch" Grid.Column="1">
Rudra
  • 148
  • 1
  • 10