gpt4 book ai didi

wpf - 当文本太长时自动垂直扩展文本框

转载 作者:行者123 更新时间:2023-12-02 02:23:28 26 4
gpt4 key购买 nike

我正在尝试模仿 Windows 7 中的便笺应用程序。在原始应用程序中,如果您在便笺中键入文本并且文本变得太大(垂直,如行数)以适应窗口,窗口自动垂直扩展,一次一行,以留出更多空间。换句话说,在普通文本框中会出现一个垂直滚动条并且文本会向下滚动(这样第一行就变得不可见),而在便签中文本框会完全扩展以适合文本,因此不会出现滚动条。当然,当您之后手动调整窗口大小时,滚动条仍然会出现。

如果您使用的是 Windows 7,只需打开便笺应用程序并在便笺中键入几行,直到它变大。

我试图模仿这种效果,但我没有运气。问题似乎是实际窗口应该调整大小,而不仅仅是文本框(我不认为 WPF 是这样工作的,子元素的大小调整可以“强制”父元素变大?至少不是窗口,对吧?)。

此时Window的内容是这样的:

<Window Background="Transparent" BorderBrush="Transparent"> 
<!-- Transparent border to draw dropshadow on -->
<Border Background="Transparent" BorderBrush="Transparent">
<!-- Grid with UI elements -->
<Grid Margin="5" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="27" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<!-- Stickynote header -->
<Border ... />

<!-- Content -->
<Border Grid.Row="1">

<TextBox Text="{Binding ContentText}" ... />

</Border>
</Grid>
</Border>
</Window>

有人知道我怎样才能达到这个效果吗?谢谢!

最佳答案

试试窗口属性 SizeToContent="Height"

样本

<Window ...
MaxHeight="500"
SizeToContent="Height">
<Border Background="Transparent" BorderBrush="Transparent">
<Grid Margin="5" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="27" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Border Grid.Row="1">
<TextBox AcceptsReturn="True" MinHeight="100"/>
</Border>
</Grid>
</Border>
</Window>

编辑要将它与您发布的 TransparentWindow 一起使用,请在 OnDragDelta (TransparentWindow.cs) 中添加 transparentWindow.SizeToContent = SizeToContent.Manual

private static void OnDragDelta(object sender, DragDeltaEventArgs e)
{
TransparentWindow transparentWindow = (TransparentWindow)sender;
Thumb thumb = e.OriginalSource as Thumb;
transparentWindow.SizeToContent = SizeToContent.Manual;
if (thumb != null && transparentWindow.WindowState == WindowState.Normal)
{
//...
}
}

关于wpf - 当文本太长时自动垂直扩展文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7057147/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com