惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
V2EX
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
博客园_首页
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
A
About on SuperTechFans
T
The Blog of Author Tim Ferriss
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
P
Proofpoint News Feed
博客园 - 司徒正美
有赞技术团队
有赞技术团队
Engineering at Meta
Engineering at Meta
Last Week in AI
Last Week in AI
MongoDB | Blog
MongoDB | Blog

博客园 - wuty007

我让AI帮忙做了一个“全国产品质量鉴定法规监测”Skill WPF + OpenCvSharp 实现摄像头、RTSP 人脸与动物检测跟踪 TelegramConsole C# 范围运算符 C# 调用WGC 实现桌面屏幕的捕获 完善基于WPF开发的标尺控件(含实例代码) C# 依赖注入 Microsoft.Extensions.DependencyInjection 实现 控制反转(IOC) C# 获取Windows系统的设备名称 记录 Windows系统开启hyper-v ,部分端口被保留,导致端口不能使用而报错的问题 WPF 调用 Win32的SetWindowDisplayAffinity 函数 实现捕获屏幕时,过滤指定的窗口 记录WPF 在清单列表设置了UIACESS为true,没有签名的报错“从服务器返回了一个参照” WPF 的ListBox 去除默认的Item项的 鼠标hover的背景颜色 WPF 调用 ChangeWindowMessageFilterEx 修改指定窗口 (UIPI) 消息筛选器的用户界面特权隔离 记录一下 WPF进程 SendMessage 发送窗口消息进行进程间通信,存在进程权限无法接受消息的问题 记录 使用PsExec启动System权限的WPF 程序 记录 命令行的 findstr 的使用 排查Windows 下的内存使用率过高,但是任务管理器看不到进程 Everything 支持 多实例 运行 指定应用 在 控制面板或者 设置的安装应用 置灰或隐藏卸载按钮 WPF 通过RawInput 获取 系统全局触摸事件 C# 定时任务 Quartz.NET 的使用 记录一下Windows系统下的命令行参数的字符个数限制
WPF 实现支持动态调整高度的文本显示控件
wuty007 · 2025-08-15 · via 博客园 - wuty007

1、需求:有个应用在做升级的时候,显示版本信息时候,宽度的一样的情况下,需要动态高度,即是有一个最小的高度,最大的高度,超出最大高度则显示滑动条显示。

如下图所示:

2、明确需求后,开始动工:

  2.1、创建一个Border,设置  MaxHeight="503" MinHeight="207"

<Window x:Class="autoHeightControl.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:autoHeightControl"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
   

    <Grid>
        <Border
                 Width="360"
                 MaxHeight="503"
                 MinHeight="207"
                 Padding="30,10,30,20"
                  Background="Gray" 
                  CornerRadius="10"
                 x:Name="MainBorder"
                 >

            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <TextBlock
                          x:Name="ThemeTextBlock"
                          Grid.Row="0"
                          Margin="0,20,0,0"
                          HorizontalAlignment="Left"
                          FontSize="20"
                          FontWeight="Bold"
                          Text="发现新版本!" />
                <!--新版本信息-->
                <Grid Grid.Row="1">
                    <TextBlock  Text="新版本信息" TextWrapping="WrapWithOverflow" Margin="0 16 0 8"   FontSize="16"
                FontWeight="SemiBold"></TextBlock>
                </Grid>

                <!--内容显示-->
                <Grid Grid.Row="2">
                    <ScrollViewer x:Name="Viewer"
             Margin="0,10"
             CanContentScroll="False"
             HorizontalScrollBarVisibility="Disabled"
             VerticalScrollBarVisibility="Auto"
             PanningMode="VerticalOnly">
                       
                        <TextBlock x:Name="textContentBlock"  TextWrapping="Wrap" Margin="0,0,10,10" FontSize="14" 
                                Text="发哈尽快老师的发发哈扣篮大赛好发的发哈尽" />
                    </ScrollViewer>
                </Grid>

<!--按钮区域--> <Grid Grid.Row="3"> <StackPanel Margin="0,10,0,0" HorizontalAlignment="Right" VerticalAlignment="Center" Orientation="Horizontal"> <Button HorizontalAlignment="Left" Background="#E7E7E7" Content="稍后再说" Foreground="#000000" /> <Button Margin="10,0" HorizontalAlignment="Right" Background="#0052D9" Content="立即升级" Foreground="#ffffff"/> </StackPanel> </Grid> </Grid> </Border> </Grid> </Window>

发现整个控件的高度,不会随着  textContentBlock 的内容大小自动适应高度,而是以最大的高度:503px 显示,如下图所示:

image

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

动态高度博客1

  2.3、这样思路就转移到如何动态设置 MainBorder  的高度了。

由于 textContentBlock 外层套了一层 ScrollViewer,我们可以使用 ScrollViewer的 ExtentHeight 动态获取文本的高度

动态高度博客2

于是我们可以把  MainBorder 的高度 Height 与 ScrollViewer 的 ExtentHeight  关联起来

3、解决方法

  3.1、新增一个 高度转换器

 public class HeightToHeightConverter : IValueConverter
 {
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
     {
         return (double)value + 207;
     }

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
     {
         throw new NotImplementedException();
     }
 }

  3.2、Mainborder 的高度增加与ScrollViewer 的 ExtentHeight 的绑定

<Window x:Class="autoHeightControl.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:autoHeightControl"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    
   <Window.Resources>
        <local:HeightToHeightConverter x:Key="HeightToHeightConverter"></local:HeightToHeightConverter>
    </Window.Resources>

 

    <Grid>
        <Border
                 Width="360"
                 MaxHeight="503"
                 MinHeight="207"
                 Height="{Binding ElementName=Viewer,Path=ExtentHeight,Converter={StaticResource HeightToHeightConverter}}"
                    Padding="30,10,30,20"
                  Background="Gray" 
                  CornerRadius="10"
                 x:Name="MainBorder"
                 >

            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <TextBlock
                          x:Name="ThemeTextBlock"
                          Grid.Row="0"
                          Margin="0,20,0,0"
                          HorizontalAlignment="Left"
                          FontSize="20"
                          FontWeight="Bold"
                          Text="发现新版本!" />
                <!--新版本信息-->
                <Grid Grid.Row="1">
                    <TextBlock  Text="新版本信息" TextWrapping="WrapWithOverflow" Margin="0 16 0 8"   FontSize="16"
                FontWeight="SemiBold"></TextBlock>
                </Grid>

                <!--内容显示-->
                <Grid Grid.Row="2" Background="Red">

                  
                    <ScrollViewer x:Name="Viewer"
                                  Margin="0,10"
                                  CanContentScroll="False"
                                  HorizontalScrollBarVisibility="Disabled"
                                  VerticalScrollBarVisibility="Auto"
                                  PanningMode="VerticalOnly">
                        <TextBlock x:Name="textBlock"  TextWrapping="Wrap" Margin="0,0,10,10" FontSize="14" 
                                   Text="快老师的发发哈扣篮大赛好发的发哈尽哈扣篮大赛好发的发哈尽发哈尽快老师的发发哈扣篮大赛好发的发哈尽哈哈扣篮大赛好发的发哈尽发哈尽快老师的发发哈扣篮大赛好发的发哈尽哈扣篮大赛好发的发哈尽发哈尽快老师的发发哈扣篮大赛好发的发哈尽扣篮大赛好发的发哈尽快老师的发发哈扣篮大赛好发的发哈尽哈扣篮大赛好发的发哈尽发哈尽快老师的发发哈扣篮大赛好发的发哈尽哈哈扣篮大赛好发的发哈尽发哈尽快老师的发发哈扣篮大赛好发的发哈尽哈扣篮大赛好发的发哈尽发哈尽快老师的发发哈扣篮大赛好发的发哈尽扣篮大赛好发的发哈尽" />

                    </ScrollViewer>
                </Grid>

                <!--按钮区域-->

                <Grid Grid.Row="3">
                    <StackPanel
         Margin="0,10,0,0"
         HorizontalAlignment="Right"
         VerticalAlignment="Center"
         Orientation="Horizontal">
                        <Button
                                 HorizontalAlignment="Left"
             Background="#E7E7E7"
             Content="稍后再说"
             Foreground="#000000"
                         />

                        <Button
             Margin="10,0"
             HorizontalAlignment="Right"
             Background="#0052D9"
             Content="立即升级"
             Foreground="#ffffff"/>
                    </StackPanel>
                </Grid>
            </Grid>
        </Border>
    </Grid>
</Window>

  3.3、效果如下图:

动态高度博客3

代码地址:wutyDemo/autoHeightControl at main · wutangyuan/wutyDemo 

参考资料:

ScrollViewer.ExtentHeight 属性 (System.Windows.Controls) | Microsoft Learn