如何为 TextBlock 设置正确的换行样式?

发布于 2025-01-07 11:12:39 字数 2466 浏览 2 评论 0原文

我需要将完整的换行文本写入 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:
enter image description here

It is done by hardcoded value Width="300"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

め可乐爱微笑 2025-01-14 11:12:39

我假设这与您的其他问题有关将一个控件的MaxWidth绑定到另一个控件的 ActualWidth

在您的情况下,我会将 Horizo​​ntal StackPanel 替换为 Grid 因为 Grid 会限制其子项的大小到可用空间,而 StackPanel 则没有。这样,TextBlock 就不需要设置 MaxWidth 属性来正确换行。

I'm assuming this is related to your other question about binding the MaxWidth of one control to the ActualWidth of another control

In your situation, I'd replace the Horizontal StackPanel with a Grid because a Grid will limit the size of it's children to the available space, while a StackPanel will not. This way the TextBlock won't need the MaxWidth property set to wrap correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文