adminSoftDK
adminSoftDK

Reputation: 2092

How to set a width of a grid column so it gets only as big as it needs to be. Auto does not work in my case

I have a user control which contains combobox only, it looks like this

<UserControl x:Class="LayoutProblem.ComboboxUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ComboBox Width="300" Height="30" VerticalAlignment="Top" HorizontalAlignment="Left"/>
</UserControl>

Then inside of another user control I add a label to it, so it looks like this

<Grid >
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Label Content="Short Label" HorizontalAlignment="Right" />
    <layoutProblem:ComboboxUserControl Grid.Column="1" />
    <Label Content="Extra long Label" HorizontalAlignment="Right" Grid.Row="1"/>
    <layoutProblem:ComboboxUserControl Grid.Column="1" Grid.Row="1"/>
</Grid>

Then next user control looks like this

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Label HorizontalAlignment="Right" Content="Some other label" />
    <TextBox Grid.Column="1"
             Width="50"
             Height="30"
             HorizontalAlignment="Left"
             VerticalAlignment="Top" />
</Grid>

Finally in MainWindow I put above together and it looks like this

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto" />
        <RowDefinition Height="auto" />
    </Grid.RowDefinitions>
    <layoutProblem:InputUserControl/>
    <layoutProblem:ComboboxWithLabel Grid.Row="1" Grid.ColumnSpan="2" DataContext="{Binding DataContext2}" />
</Grid> 

So all of this puts the controls together, but I’m having trouble of how to change the layout. Because now the labels should be in column 1 and textbox and comboboxes should be put to column 2. Itoes not matter what I set to the column definitions to, Auto, * or nothing the columns don’t get the size I want. If I take the column definitions out it suddenly works, but I need them because there are other things which are going to be put in the second column.

I looked online but I haven’t found a way to do it.

Thank you for your help.

Upvotes: 1

Views: 153

Answers (1)

adminsoft
adminsoft

Reputation: 38

Solved using IsSharedSizeScope = True and SharedSizeGroup = "".

Upvotes: 1

Related Questions