views:

1070

answers:

2

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>
+2  A: 

I've tried to replicate your problem by pasting everything between your Grid elements in to Kaxaml but everything wraps as you would expect it to. (I inserted regular strings where you were doing bindings and removed the Label style).

It could be that the problem is higher up the tree.

I'd suggest pasting chunks in to Kaxaml or similar to test and see which parent breaks your UI.

Nidonocu
A: 

I am having this problem too... did you ever figure it out? I have several nested grids with an outside column of Width="*" and inner one with Width="Auto" and a MaxWidth of some test value like 400. My idea was to bind the MaxWidth of the inner (auto) column to the ActualWidth of the outer column, but it wasn't working so I tried a static test value which doesn't work either!

chaiguy
Hmm, I seem to have solved it but I don't know how. By adding a `HorizontalAlignment="Left"` to the inner grid, and setting the inner grid's column to `Width="*"`, it works! I can't understand the logic of this, but hey it works, and I didn't even have to use binding! :P
chaiguy
I haven't touched WPF in over a year so I'm not much help, sorry :/ Good luck!
Trevor Hartman