我有一个包含网格的列表框。Grid 有一个 Label Column、一个 GridSplitter 和一个 TextBox Column。如何让 Grid 占据 ListBox 的整个 Width 并使 TextBox 使用 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>
参数只是为了演示这个想法:
public class Parameter
{
public string Key { get; set; }
public string Value { get; set; }
public Parameter(string key, string value)
{
Key = key;
Value = value;
}
}