Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Grid Column MaxWidth not enforced

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>
like image 392
devth Avatar asked Feb 11 '09 22:02

devth


2 Answers

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.

like image 125
Nidonocu Avatar answered Sep 17 '22 17:09

Nidonocu


I provided an answer to this question, only it was using an ListView instead of an ItemsControl but the issue is likely the same. There is probably a ScrollViewer surrounding your ItemPresenter and you need to edit a copy of the ItemsControl template.

WPF ListView TextBlock TextWrapping

like image 36
Rhyous Avatar answered Sep 20 '22 17:09

Rhyous