WPF Grid Column MaxWidth not enforced

Posted by Trevor Hartman on Stack Overflow See other posts from Stack Overflow or by Trevor Hartman
Published on 2009-02-11T22:35:31Z Indexed on 2010/04/14 21:53 UTC
Read the original article Hit count: 327

Filed under:
|
|
|

This problem stems from not being able to get my TextBlock to wrap. Basically as a last-ditch attempt I am setting MaxWidth on my container grid's columns. I was surprised to find that my child label and textbox still do whatever they want (bad children, BAD) and are not limited by my grid column's MaxWidth="200".

What I'm really trying to do is let my TextBlock fill available width and wrap if necessary. So far after trying many variations of HorizontalAlignment="Stretch" on every known parent in the universe, nothing works, except setting an explicit MaxWidth="400" or whatever number on the TextBlock. This is not good because I need the TextBlock to fill available width, not be limited by some fixed number. Thanks!

<ItemsControl>
   <ItemsControl.ItemsPanel>
       <ItemsPanelTemplate>
           <StackPanel />
       </ItemsPanelTemplate>
   </ItemsControl.ItemsPanel>
   <ItemsControl.ItemTemplate>
       <DataTemplate>
           <Grid>
			<Grid.ColumnDefinitions>
				<ColumnDefinition MaxWidth="200" SharedSizeGroup="A" />
				<ColumnDefinition MaxWidth="200" SharedSizeGroup="B" />
			</Grid.ColumnDefinitions>

			<Label VerticalAlignment="Top" Margin="0 5 0 0" Grid.Column="0" Style="{StaticResource LabelStyle}" Width="Auto" Content="{Binding Value.Summary}" />
			<TextBlock Grid.Column="1" Margin="5,8,5,8" FontWeight="Normal"
					   Background="AliceBlue"
					   Foreground="Black" Text="{Binding Value.Description}" 
					   HorizontalAlignment="Stretch"
					   TextWrapping="Wrap" Height="Auto" />
           </Grid>
       </DataTemplate>
   </ItemsControl.ItemTemplate>
</ItemsControl>

© Stack Overflow or respective owner

Related posts about wpf

Related posts about layout