在 WPF 中运行时调整文本框大小

发布于 2024-09-01 17:04:10 字数 92 浏览 1 评论 0原文

只是想知道如何让用户在运行时通过拖动 WPF 中的角来调整 TextBox 控件的大小。不太重要的是,是否使用相同的技术来调整所有控件的大小?

谢谢 :)

Was just wondering how I would go about letting the user resize a TextBox control at runtime by dragging its corners in WPF. Less importantly, is the same technique used for the resizing of all controls?

Thank you :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只是一片海 2024-09-08 17:04:10

您应该尝试将文本框的对齐方式设置为拉伸并将其放置在可以调整大小的容器中,例如带有网格分割器的网格(或在可调整大小的窗口中)。这比尝试创建自定义可调整大小的文本框要容易得多,并且它可以更好地与布局的其余部分配合使用。

编辑:
这是来自真实应用程序的示例:

<Grid>...
<GridSplitter Grid.Row="1" Grid.ColumnSpan="2" ResizeDirection="Rows" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Height="4" BorderThickness="0,0,0,1" BorderBrush="Gray" Background="Transparent"/>
<TextBox Grid.Row="2" Grid.Column="0" Margin="6,6,6,6" Name="RequestTextBox" VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" Text="{Binding Request, Mode=TwoWay}"/>
<GridSplitter Grid.Row="2" Grid.ColumnSpan="2" ResizeDirection="Rows" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Height="4" BorderThickness="0,0,0,1" BorderBrush="Gray" Background="Transparent"/>
...</Grid>

You should try setting the textbox's alignments to stretch and placing it inside a container that you can resize, like a grid with gridsplitters (or in a resizeable window). It's much easier than trying to create a custom resizeable textbox, and it will work better with the rest of your layout.

EDIT:
Here's an example from a real app:

<Grid>...
<GridSplitter Grid.Row="1" Grid.ColumnSpan="2" ResizeDirection="Rows" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Height="4" BorderThickness="0,0,0,1" BorderBrush="Gray" Background="Transparent"/>
<TextBox Grid.Row="2" Grid.Column="0" Margin="6,6,6,6" Name="RequestTextBox" VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" Text="{Binding Request, Mode=TwoWay}"/>
<GridSplitter Grid.Row="2" Grid.ColumnSpan="2" ResizeDirection="Rows" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Height="4" BorderThickness="0,0,0,1" BorderBrush="Gray" Background="Transparent"/>
...</Grid>
倚栏听风 2024-09-08 17:04:10

tehMick 的答案是绝对正确的:您绝对应该创建一个容器来调整大小,而不是自定义 TextBox 本身。如果它适合您,GridSplitter 就是一个非常好的现成解决方案。

我遇到了同样的情况,但 GridSplitter 不起作用,因此我创建了一个“ResizeBorder”控件,用于处理鼠标拖动其四个角以在二维中调整大小,或在两侧的中间拖动以在一维中调整大小。这实际上是非常简单的代码:只需处理 MouseDown,设置一个局部变量,给出 MouseDown 位置和被拖动的边/角,然后在 MouseMove 上更新大小。

我的 ResizeBorder 是可设计的,因此我可以在角落处显示四个框,在侧面显示线条,或者任何我能想到的更复杂的东西。

另请注意,无论您使用的是 Grid 和 GridSplitters 还是 ResizeBorder 或其他任何东西,您都可以选择将调整大小功能放在控件周围,如下所示:

<my:ResizeBorder ...>
  <TextBox ... />
</my:ResizeBorder>

或者通过更新 TextBox 本身的 ControlTemplate :

<ControlTemplate x:Key="ResizableTextBox" TargetType="{x:Type TextBox}">
  <my:ResizeBorder>
    ...
  </my:ResizeBorder>
</ControlTemplate>

...

<TextBox Template="{StaticResource ResizableTextBoxTemplate}" ... />

后一种方法的优点是您可以使用样式或附加属性来调整文本框的大小,并且可以轻松地在代码中动态更改文本框的大小可调整性。

tehMick's answer is absolutely correct: You should definitely create a container to do the resizing rather than customizing the TextBox itself. And if it works for you GridSplitter is a very good in-the-box solution.

I had the same situation but GridSplitter wouldn't work, so I created a "ResizeBorder" control that handled mouse drags on its four corners to resize in two dimensions, or the middle of the sides to resize in one. This is actually very simple code: Just handle MouseDown, set a local variable giving the MouseDown location and the side/corner being dragged, then on MouseMove update the size.

My ResizeBorder was stylable so I could show just four boxes at the corners and lines on the sides, or anything more complex that I could dream up.

Also, note that whether you are using a Grid and GridSplitters or a ResizeBorder or anything else, you have the choice of putting your resize functionality either around the control like this:

<my:ResizeBorder ...>
  <TextBox ... />
</my:ResizeBorder>

or by updating the ControlTemplate for TextBox itself:

<ControlTemplate x:Key="ResizableTextBox" TargetType="{x:Type TextBox}">
  <my:ResizeBorder>
    ...
  </my:ResizeBorder>
</ControlTemplate>

...

<TextBox Template="{StaticResource ResizableTextBoxTemplate}" ... />

The advantages of this latter method are that you can use a style or attached property to make may TextBoxes resizable and that you can easily change the resizability of the TextBox dynamically in code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文