简体   繁体   中英

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;
   }
}

尝试在ListBox上设置HorizontalContentAlignment="Stretch"

不要给TextBox提供任何宽度和高度。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM