How to refer MaxWidth=“??” of TextBlock to Owner ActualWidth?

那年仲夏 提交于 2019-12-06 16:54:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!