【问题标题】:Is it possible to make text wrap inside of a border? WPF是否可以使文本在边框内换行? WPF
【发布时间】:2021-12-14 18:13:11
【问题描述】:

我有这样的事情:

<ControlTemplate>
                <StackPanel Orientation="Horizontal">
                    <!--Icon-->
                        <Ellipse Width="30" Height="30" Margin="10,0,0,-5">
                        <Ellipse.Fill>
                            <ImageBrush ImageSource="{Binding ImageSource}"
                                        RenderOptions.BitmapScalingMode="Fant"/>
                        </Ellipse.Fill>
                    </Ellipse>
                    
                    <!--Surround Info-->
                    <StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <Label Content="{Binding Name}"
                                      Foreground="{Binding UserNameColor}"
                                      FontWeight="SemiBold"
                                      VerticalAlignment="Center"
                                      Margin="0,0,-5,0"/>
                            <Label Content="{Binding Date}"
                                      Foreground="White"
                                      FontWeight="SemiBold"
                                      FontSize="8"
                                      VerticalAlignment="Center"
                                      Margin="0,0,-5,0"/>
                        </StackPanel>
                        
                        <!--Message-->

                        
                        <Border VerticalAlignment="Center" CornerRadius="10 15 15 0" Margin="2 2 88 3" Background="LightSlateGray">
                            <TextBlock Foreground="White" Margin="7" TextWrapping="Wrap" FontWeight="SemiBold" Text="{Binding Content}"/>
                        </Border>
                    </StackPanel>
                </StackPanel>
            </ControlTemplate>

我需要文本块用里面的文本换行,但我不知道如何在不提供我不想要的边框宽度的情况下这样做

我想实现这样的目标:

【问题讨论】:

  • 不清楚你想要实现什么。试着解释你的问题,让每个人都能理解。同时展示您已经尝试过的内容。
  • 听起来你想要一个设置了圆角半径的边框,然后在里面放一个带边距的文本块,并且不知道为什么要使用 controltemplate (sus)
  • 我是 wpf 的新手,我需要将边框内的文本块进行换行,但它不是,我认为是因为边框,我不知道如何解决这个问题跨度>

标签: c# wpf xaml


【解决方案1】:

您似乎在 StackPanels 中使用 StackPanels。 有一个已知问题 StackPanel 将收到其父级的调整大小事件,但仅在大小增加时。 参考:WPF grid size changed event firing only when increasing and not when decreasing

我建议使用其他元素,例如 Grid。

<Window x:Class="WpfSandbox.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"                
    Title="MainWindow" Height="450" Width="300">
<Grid Background="#292B2F" x:Name="OuterGrid">
    <!--Surround Info-->
    <Grid Margin="10">
        <StackPanel>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Label Content="Name" Foreground="Gray" FontWeight="SemiBold" VerticalAlignment="Center" Margin="0,0,-5,0" />
                <Label Content="2021/10/29" Grid.Column="1" Foreground="White" FontWeight="SemiBold" FontSize="8" VerticalAlignment="Center" Margin="5,0,-5,0" />
            </Grid>

            <!--Message-->
            <Border VerticalAlignment="Center" CornerRadius="10 15 15 0" Margin="2 2 20 3" Background="LightSlateGray">
                <TextBlock Foreground="White" Margin="7" TextWrapping="Wrap" FontWeight="SemiBold"
                           Text="Blah blah blah yadda yadda yadda blah blah blah this is a really really long message that should wrap at some point, giving you the functionality you are after." />
            </Border>
        </StackPanel>
    </Grid>
</Grid>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-03
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    相关资源
    最近更新 更多