R – WPF Grid Column MaxWidth not enforced

gridlayoutword-wrapwpf

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>

Best Answer

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.

Related Topic