找回密码
 立即注册
首页 业界区 业界 WPF 实现支持动态调整高度的文本显示控件 ...

WPF 实现支持动态调整高度的文本显示控件

句惫 2025-8-15 20:12:33
1、需求:有个应用在做升级的时候,显示版本信息时候,宽度的一样的情况下,需要动态高度,即是有一个最小的高度,最大的高度,超出最大高度则显示滑动条显示。
如下图所所示:
1.png

 
2、明确需求后,开始动工:
  2.1、创建一个Border,设置  MaxHeight="503" MinHeight="207"
  1. <Window x:Class="autoHeightControl.MainWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6.         xmlns:local="clr-namespace:autoHeightControl"
  7.         mc:Ignorable="d"
  8.         Title="MainWindow" Height="450" Width="800">
  9.    
  10.     <Grid>
  11.         <Border
  12.                  Width="360"
  13.                  MaxHeight="503"
  14.                  MinHeight="207"
  15.                  Padding="30,10,30,20"
  16.                   Background="Gray"
  17.                   CornerRadius="10"
  18.                  x:Name="MainBorder"
  19.                  >
  20.             <Grid>
  21.                 <Grid.RowDefinitions>
  22.                     <RowDefinition Height="Auto" />
  23.                     <RowDefinition Height="Auto" />
  24.                     <RowDefinition Height="*"/>
  25.                     <RowDefinition Height="Auto" />
  26.                 </Grid.RowDefinitions>
  27.                 <TextBlock
  28.                           x:Name="ThemeTextBlock"
  29.                           Grid.Row="0"
  30.                           Margin="0,20,0,0"
  31.                           HorizontalAlignment="Left"
  32.                           FontSize="20"
  33.                           FontWeight="Bold"
  34.                           Text="发现新版本!" />
  35.                
  36.                 <Grid Grid.Row="1">
  37.                     <TextBlock  Text="新版本信息" TextWrapping="WrapWithOverflow" Margin="0 16 0 8"   FontSize="16"
  38.                 FontWeight="SemiBold"></TextBlock>
  39.                 </Grid>
  40.                
  41.           <strong>      <Grid Grid.Row="2">
  42.                     <ScrollViewer x:Name="Viewer"
  43.              Margin="0,10"
  44.              CanContentScroll="False"
  45.              HorizontalScrollBarVisibility="Disabled"
  46.              VerticalScrollBarVisibility="Auto"
  47.              PanningMode="VerticalOnly">
  48.                        
  49.                         <TextBlock x:Name="textContentBlock"  TextWrapping="Wrap" Margin="0,0,10,10" FontSize="14"
  50.                                 Text="发哈尽快老师的发发哈扣篮大赛好发的发哈尽" />
  51.                     </ScrollViewer>
  52.                 </Grid></strong>
  53. <br><br><br>
  54.                
  55.                 <Grid Grid.Row="3">
  56.                     <StackPanel
  57.          Margin="0,10,0,0"
  58.          HorizontalAlignment="Right"
  59.          VerticalAlignment="Center"
  60.          Orientation="Horizontal">
  61.                         <Button
  62.                                  HorizontalAlignment="Left"
  63.              Background="#E7E7E7"
  64.              Content="稍后再说"
  65.              Foreground="#000000"
  66.                          />
  67.                         <Button
  68.              Margin="10,0"
  69.              HorizontalAlignment="Right"
  70.              Background="#0052D9"
  71.              Content="立即升级"
  72.              Foreground="#ffffff"/>
  73.                     </StackPanel>
  74.                 </Grid>
  75.             </Grid>
  76.         </Border>
  77.     </Grid>
  78. </Window>
复制代码
发现整个控件的高度,不会随着  textContentBlock 的内容大小自动适应高度,而是以最大的高度:503px 显示,如下图所示:
2.png

   2.2、使用Snoop 监控MainBorder ,发现只有手动设置 最外层的  MainBorder 的 Height 属性才能修改整个控件的高度,而在超出MaxHeight ,则按MaxHeight 显示,小于MinHeight 则按 MinHeight显示
3.gif

 
  2.3、这样思路就转移到如何动态设置 MainBorder  的高度了。
由于 textContentBlock 外层套了一层 ScrollViewer,我们可以使用 ScrollViewer的 ExtentHeight 动态获取文本的高度
4.gif

 
于是我们可以把  MainBorder 的高度 Height 与 ScrollViewer 的 ExtentHeight  关联起来
 
 
3、解决方法
  3.1、新增一个 高度转换器
  1. public class HeightToHeightConverter : IValueConverter
  2. {
  3.      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  4.      {
  5.          return (double)value + 207;
  6.      }
  7.      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  8.      {
  9.          throw new NotImplementedException();
  10.      }
  11. }
复制代码
 
  3.2、Mainborder 的高度增加与ScrollViewer 的 ExtentHeight 的绑定
  1. <Window x:Class="autoHeightControl.MainWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6.         xmlns:local="clr-namespace:autoHeightControl"
  7.         mc:Ignorable="d"
  8.         Title="MainWindow" Height="450" Width="800">
  9.    
  10.    <Window.Resources>
  11.         <local:HeightToHeightConverter x:Key="HeightToHeightConverter"></local:HeightToHeightConverter>
  12.     </Window.Resources>
  13.     <Grid>
  14.         <Border
  15.                  Width="360"
  16.                  MaxHeight="503"
  17.                  MinHeight="207"<strong>
  18.                  Height="{Binding ElementName=Viewer,Path=ExtentHeight,Converter={StaticResource HeightToHeightConverter}}"</strong>
  19.                     Padding="30,10,30,20"
  20.                   Background="Gray"
  21.                   CornerRadius="10"
  22.                  x:Name="MainBorder"
  23.                  >
  24.             <Grid>
  25.                 <Grid.RowDefinitions>
  26.                     <RowDefinition Height="Auto" />
  27.                     <RowDefinition Height="Auto" />
  28.                     <RowDefinition Height="*"/>
  29.                     <RowDefinition Height="Auto" />
  30.                 </Grid.RowDefinitions>
  31.                 <TextBlock
  32.                           x:Name="ThemeTextBlock"
  33.                           Grid.Row="0"
  34.                           Margin="0,20,0,0"
  35.                           HorizontalAlignment="Left"
  36.                           FontSize="20"
  37.                           FontWeight="Bold"
  38.                           Text="发现新版本!" />
  39.                
  40.                 <Grid Grid.Row="1">
  41.                     <TextBlock  Text="新版本信息" TextWrapping="WrapWithOverflow" Margin="0 16 0 8"   FontSize="16"
  42.                 FontWeight="SemiBold"></TextBlock>
  43.                 </Grid>
  44.                
  45.                 <Grid Grid.Row="2" Background="Red">
  46.                   
  47.                     <ScrollViewer x:Name="Viewer"
  48.                                   Margin="0,10"
  49.                                   CanContentScroll="False"
  50.                                   HorizontalScrollBarVisibility="Disabled"
  51.                                   VerticalScrollBarVisibility="Auto"
  52.                                   PanningMode="VerticalOnly">
  53.                         <TextBlock x:Name="textBlock"  TextWrapping="Wrap" Margin="0,0,10,10" FontSize="14"
  54.                                    Text="快老师的发发哈扣篮大赛好发的发哈尽哈扣篮大赛好发的发哈尽发哈尽快老师的发发哈扣篮大赛好发的发哈尽哈哈扣篮大赛好发的发哈尽发哈尽快老师的发发哈扣篮大赛好发的发哈尽哈扣篮大赛好发的发哈尽发哈尽快老师的发发哈扣篮大赛好发的发哈尽扣篮大赛好发的发哈尽快老师的发发哈扣篮大赛好发的发哈尽哈扣篮大赛好发的发哈尽发哈尽快老师的发发哈扣篮大赛好发的发哈尽哈哈扣篮大赛好发的发哈尽发哈尽快老师的发发哈扣篮大赛好发的发哈尽哈扣篮大赛好发的发哈尽发哈尽快老师的发发哈扣篮大赛好发的发哈尽扣篮大赛好发的发哈尽" />
  55.                     </ScrollViewer>
  56.                 </Grid>
  57.                
  58.                 <Grid Grid.Row="3">
  59.                     <StackPanel
  60.          Margin="0,10,0,0"
  61.          HorizontalAlignment="Right"
  62.          VerticalAlignment="Center"
  63.          Orientation="Horizontal">
  64.                         <Button
  65.                                  HorizontalAlignment="Left"
  66.              Background="#E7E7E7"
  67.              Content="稍后再说"
  68.              Foreground="#000000"
  69.                          />
  70.                         <Button
  71.              Margin="10,0"
  72.              HorizontalAlignment="Right"
  73.              Background="#0052D9"
  74.              Content="立即升级"
  75.              Foreground="#ffffff"/>
  76.                     </StackPanel>
  77.                 </Grid>
  78.             </Grid>
  79.         </Border>
  80.     </Grid>
  81. </Window>
复制代码
  3.3、效果如下图:
5.gif

 
参考资料:
ScrollViewer.ExtentHeight 属性 (System.Windows.Controls) | Microsoft Learn
 

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册