问题
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>
回答1:
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>
来源:https://stackoverflow.com/questions/9362291/how-to-refer-maxwidth-of-textblock-to-owner-actualwidth