文本过长时自动垂直扩展文本框
我正在尝试模仿 Windows 7 中的粘滞便笺应用程序。在原始应用程序中,如果您在粘滞便笺中键入文本并且文本变得太大(垂直方向,如行数)而无法适合窗口,则窗口会自动垂直扩展,一次一行,以留出更多空间。换句话说,在普通文本框中会出现垂直滚动条,并且文本会向下滚动(以便第一行变得不可见),而在粘滞便笺中,文本框会完全扩展以适合文本,因此不会出现滚动条。当然,当您随后手动调整窗口大小时,滚动条仍然会出现。
如果您使用的是 Windows 7,只需打开便笺应用程序并在便笺中键入几行,直到其放大。
我试图模仿这种效果,但我没有运气。问题似乎是实际的 Window 应该调整大小,而不仅仅是 Textbox (我不认为 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>
有谁知道我怎样才能达到这个效果?谢谢!
I am trying to mimic the Sticky Notes application in Windows 7. In the original application, if you type text into a Sticky Note and the text becomes too large (vertically, as in number of lines) to fit in the window, the window automatically expands vertically, one line at a time, to allow for more room. In other words, where in a normal Textbox a vertical scrollbar would appear and the text would scroll down (so that the first line becomes invisible), in the Sticky Notes the textbox expands exactly enough to fit the text so that no scrollbar appears. The scrollbar still appears when you manually resize the window afterwards, of course.
If you have Windows 7 just open the Sticky Notes application and type a few lines in the sticky note until it enlarges.
I am trying to mimic this effect but I'm having no luck. The problem seems that the actual Window should resize, not just the Textbox (I don't think WPF works this way, that a resize of a child element can 'force' the parent element to become larger? At least not for a Window, right?).
The contents of the Window at this point are such:
<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>
Does anybody know how I can achieve this effect? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试窗口属性
SizeToContent="Height"
示例
编辑
要将其与您发布的
TransparentWindow
一起使用,请在OnDragDelta
(TransparentWindow.cs) 中添加transparentWindow.SizeToContent = SizeToContent.Manual
Try the Window Property
SizeToContent="Height"
Sample
Edit
To use it with the
TransparentWindow
you posted, addtransparentWindow.SizeToContent = SizeToContent.Manual
inOnDragDelta
(TransparentWindow.cs)