Search code examples
c#wpftextboxgrid

Let TextBox fill remaining Space in Grid


I have a ListBox that contains a Grid. The Grid has a Label Column, a GridSplitter and a TextBox Column. How can I make the Grid take up the entire Width of the ListBox and make the TextBox use the entire space of the Grid Column?

<Grid>
    <ListBox x:Name="myList"  Margin="4" ItemsSource="{Binding Path=Parameter}" MinHeight="60" Grid.IsSharedSizeScope="True" HorizontalAlignment="Stretch" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid x:Name="parameterGrid" HorizontalAlignment="Stretch">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition x:Name="labelColumn" Width="Auto" SharedSizeGroup="LabelColumn"/>
                        <ColumnDefinition x:Name="splitterColumn" Width="5"/>
                        <ColumnDefinition x:Name="textColumn" Width="*"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding Path=Key}" Grid.Column="0"/>
                    <GridSplitter Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
                    <TextBox Grid.Column="2" Text="{Binding Path=Value, UpdateSourceTrigger=LostFocus}"
                                                                 IsEnabled="{Binding ElementName=_view, Path=DataContext.IsEditEnabled}">
                    </TextBox>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

The Parameter is just to demonstrate the idea:

public class Parameter
{ 
   public string Key { get; set; }
   public string Value { get; set; }

   public Parameter(string key, string value)
   {
       Key = key;
       Value = value;
   }
}

Solution

  • Try setting HorizontalContentAlignment="Stretch" on the ListBox.