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

推荐订阅源

TaoSecurity Blog
TaoSecurity Blog
L
LINUX DO - 最新话题
Help Net Security
Help Net Security
N
News | PayPal Newsroom
www.infosecurity-magazine.com
www.infosecurity-magazine.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
The Last Watchdog
The Last Watchdog
S
Security @ Cisco Blogs
W
WeLiveSecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
Webroot Blog
Webroot Blog
T
Troy Hunt's Blog
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
N
News and Events Feed by Topic
T
Threat Research - Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tor Project blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
D
Darknet – Hacking Tools, Hacker News & Cyber Security
PCI Perspectives
PCI Perspectives
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Apple Machine Learning Research
Apple Machine Learning Research
IT之家
IT之家
S
SegmentFault 最新的问题
J
Java Code Geeks
P
Privacy & Cybersecurity Law Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 【当耐特】
博客园_首页
H
Hacker News: Front Page
T
Threatpost
Jina AI
Jina AI
博客园 - Franky
月光博客
月光博客
L
LINUX DO - 热门话题
The Cloudflare Blog
H
Heimdal Security Blog
博客园 - 司徒正美
酷 壳 – CoolShell
酷 壳 – CoolShell
Cloudbric
Cloudbric
雷峰网
雷峰网
Hugging Face - Blog
Hugging Face - Blog
S
Secure Thoughts
T
Tenable Blog
I
Intezer
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

博客园 - wuty007

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