3

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;
   }
}
tobre
  • 1,347
  • 3
  • 21
  • 53

2 Answers2

10

Try setting HorizontalContentAlignment="Stretch" on the ListBox.

LPL
  • 16,827
  • 6
  • 51
  • 95
Janushirsha
  • 141
  • 1
  • 3
  • Wow! So simple. You would not believe how many things I tried. But this was not part of it. Thank you very much! – tobre May 03 '12 at 06:43
1

Dont give any Width and Height for TextBox.

Kishore Kumar
  • 12,675
  • 27
  • 97
  • 154