0
votes

I have a feeling there is a simple solution but I'm just stuck.

In XAML I have a Grid with auto size, with several Rows, with TextBlock in each Row.
I need one of the TextBlocks to restrict it's Width so it doesn't stretch the Grid, while let the other ones to stretch and define Width of the Grid (= of the whole Control).
Or in other words so one TextBlock Width (MaxWidth) is set to Width of widest of the remaining TextBlocks.
Of course the restricted TextBlock has a text Wrap, the other ones don't.

When I just set the restricted TextBlock HorizontalAlignment="Left" (or anything else then Stretch) and TextWrapping="Wrap" the tblock still takes all space it can.

I can't simply bind the tblock's Width to some other element Width, because I need largest of several Widths.

The only solution I was able to think of..
I can derive from TextBlock, override OnArrange, somehow get sizes of its neighbor tblocks and set its size accordingly.
Or I can create a Converter and do basically similar as above with multi biding.

Thought it seems like I'm killing a mosquito with bulldozer and theres some simple solution I missed (?)

Edit: sample code

<Grid x:Name="rootGrid" HorizontalAlignment="Center" VerticalAlignment="Center" MinWidth="64" MinHeight="64" >

    <Rectangle x:Name="bgRect" Fill="Gray" RadiusX="6" RadiusY="6" Margin="1" Grid.RowSpan="2" />

    <Grid x:Name="contentGrid" Grid.Row="1" Margin="8,0,8,6" >
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>

        <TextBlock x:Name="textName" Text="Gill Bates junior" FontWeight="Bold" FontSize="14" />

        <TextBlock x:Name="textDesc" TextWrapping="Wrap" Grid.Row="1" HorizontalAlignment="Left" 
                    Text="Test test test test Test test test test Test test test test Test test test test Test test test test Test test test test Test test test test " />
        <TextBlock x:Name="textDates" Text="1995 - 2075" Grid.Row="2" />
    </Grid>
</Grid>
1
could you provide a simple code?Rang
I didn't think its really necessary in this case, but here goes.Riva

1 Answers

0
votes

I found one simple solution. Although it works in this case, I'm not sure how generally usable or good it actually is. Anyway, I set Width of the restricted TBlock to 1. It shouldn't be 0 because then the optimisation code in its layout methods might bypass some of its layout steps entirely.
Then I bound its MinWidth to its parent Grid's ActualWidth. Since the TBlock doenst have any Margins set, it gives me its desired width.
Surprisingly it worked.

Potential problems:
I originally expected it might throw the layouting system into loop. Luckily, apparently cases like this are solved in the framework, so it didn't. That doesn't mean it wount happen in different constellation.
To help it, and to help prevent any cascade resizing and re-layouting, I moved the TBlock to be last element in Grid definition in XAML, so it's layout passes are resolved as last of all Grid's elements.

I'm not sure what this circular dependency does to the layout passes (and it isn't easy to profile), but it works.