如何为 TextBlock 设置正确的换行样式?
我需要将完整的换行文本写入 WPF Textblock。条件是我们不知道文本长度以及窗口大小。
我的 XAML 是:
<Border BorderThickness="1" CornerRadius="6" BorderBrush="#FF405DEF" Padding="5" Margin="5">
<StackPanel Orientation="Horizontal">
<Image HorizontalAlignment="Left" Source="{Binding Path=User.ProfileImageUrl}" Width="48" Height="48"
Name="UserAvatarImage" Stretch="Fill" VerticalAlignment="Top" />
<StackPanel Margin="5,0,0,5" Orientation="Vertical" Grid.Column="1" HorizontalAlignment="Left" Name="stackPanel1" VerticalAlignment="Top" Width="413">
<StackPanel Orientation="Horizontal" >
<StackPanel Orientation="Horizontal" >
<TextBlock Name="UserNameTextBlock" Margin="0,0,10,0" Text="{Binding Path=User.Name}" FontWeight="Bold">UserName</TextBlock>
<TextBlock Foreground="LightGray" >@</TextBlock>
<TextBlock Name="ScreenNameTextBlock" Text="{Binding Path=User.ScreenName}" Foreground="Gray" >ScreenName</TextBlock>
</StackPanel>
</StackPanel>
<StackPanel 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"
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>
</StackPanel>
</StackPanel>
</Border>
所以我需要这个:
这是通过硬编码值Width="300"
I need to have complete wraped text to WPF Textblock. The condition is that we don't know the text length as well as window size.
The XAML I have is:
<Border BorderThickness="1" CornerRadius="6" BorderBrush="#FF405DEF" Padding="5" Margin="5">
<StackPanel Orientation="Horizontal">
<Image HorizontalAlignment="Left" Source="{Binding Path=User.ProfileImageUrl}" Width="48" Height="48"
Name="UserAvatarImage" Stretch="Fill" VerticalAlignment="Top" />
<StackPanel Margin="5,0,0,5" Orientation="Vertical" Grid.Column="1" HorizontalAlignment="Left" Name="stackPanel1" VerticalAlignment="Top" Width="413">
<StackPanel Orientation="Horizontal" >
<StackPanel Orientation="Horizontal" >
<TextBlock Name="UserNameTextBlock" Margin="0,0,10,0" Text="{Binding Path=User.Name}" FontWeight="Bold">UserName</TextBlock>
<TextBlock Foreground="LightGray" >@</TextBlock>
<TextBlock Name="ScreenNameTextBlock" Text="{Binding Path=User.ScreenName}" Foreground="Gray" >ScreenName</TextBlock>
</StackPanel>
</StackPanel>
<StackPanel 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"
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>
</StackPanel>
</StackPanel>
</Border>
So I need this:
It is done by hardcoded value Width="300"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设这与您的其他问题有关将一个控件的
MaxWidth
绑定到另一个控件的ActualWidth
在您的情况下,我会将 Horizontal
StackPanel
替换为Grid
因为 Grid 会限制其子项的大小到可用空间,而 StackPanel 则没有。这样,TextBlock
就不需要设置MaxWidth
属性来正确换行。I'm assuming this is related to your other question about binding the
MaxWidth
of one control to theActualWidth
of another controlIn your situation, I'd replace the Horizontal
StackPanel
with aGrid
because a Grid will limit the size of it's children to the available space, while aStackPanel
will not. This way theTextBlock
won't need theMaxWidth
property set to wrap correctly.