0

How to refer MaxWidth="??" of TextBlock to stpMessage ActualWidth?

<StackPanel Name="stpMessage" Orientation="Horizontal"  Margin="0,5,0,0">
                    <Grid >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="2*" />
                            <ColumnDefinition Width="2*" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Margin="0,0,0,0" Foreground="Blue" TextWrapping="Wrap">@ToUserName</TextBlock>
                        <StackPanel Grid.Column="1">
                            <TextBlock Margin="5,0,0,0" Text="{Binding Path=Text}" MinHeight="20"  MinWidth="200"  HorizontalAlignment="Stretch" MaxWidth="1000"
                                       VerticalAlignment="Stretch" TextWrapping="WrapWithOverflow">START skf skdjf skdj hfskdjf ksdjhf ksjdhf ksjhf kjsf kjshf kjshkjfhsdf kjsfdkj hskdfj hskdjf hskdjf skjhfksjfks END</TextBlock>
                        </StackPanel>    
                    </Grid>
                </StackPanel>
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
NoWar
  • 36,338
  • 80
  • 323
  • 498

1 Answers1

4

The problem with this is that StackPanels do not limit the size of their children, so will grow as much as their children need

Change your StackPanel to a control that limits the size of it's children, like a Grid (or wrap it in another control) and then use an ElementName binding to bind to the ActualWidth property of that control

<Grid Name="stpMessage" ... />
    ...
    <TextBlock MaxWidth="{Binding ElementName=stpMessage, Path=ActualWidth}" ... />
    ...
</Grid>
Rachel
  • 130,264
  • 66
  • 304
  • 490