DataTemplate中的Grid解决方法
DataTemplate中的Grid
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="Yellow" >
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding}" Grid.Row="0" Grid.Column="0" FontSize="10"/>
<TextBlock Text="{Binding}" Grid.Row="0" Grid.Column="1" FontSize="30"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
当Grid在ListBox的DataTemplate时,Grid的Width并没有充满ListBoxItem的Width,而是有多宽就扩展多宽。
如下图的黄色部分,求大神指点,如何让Grid自动充满一行啊
![DataTemplate中的Grid解决方法 DataTemplate中的Grid解决方法](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDEzLzAzLzEwLzA5NTAxOTE5MS5qcGc=)
------解决方案--------------------
横向对齐设置成 stretch.
------解决方案--------------------
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment"
Value="Stretch" />
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="Yellow" >
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding}" Grid.Row="0" Grid.Column="0" FontSize="10"/>
<TextBlock Text="{Binding}" Grid.Row="0" Grid.Column="1" FontSize="30"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
当Grid在ListBox的DataTemplate时,Grid的Width并没有充满ListBoxItem的Width,而是有多宽就扩展多宽。
如下图的黄色部分,求大神指点,如何让Grid自动充满一行啊
------解决方案--------------------
横向对齐设置成 stretch.
------解决方案--------------------
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment"
Value="Stretch" />