在 WPF 中运行时调整文本框大小
只是想知道如何让用户在运行时通过拖动 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该尝试将文本框的对齐方式设置为拉伸并将其放置在可以调整大小的容器中,例如带有网格分割器的网格(或在可调整大小的窗口中)。这比尝试创建自定义可调整大小的文本框要容易得多,并且它可以更好地与布局的其余部分配合使用。
编辑:
这是来自真实应用程序的示例:
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:
tehMick 的答案是绝对正确的:您绝对应该创建一个容器来调整大小,而不是自定义 TextBox 本身。如果它适合您,
GridSplitter
就是一个非常好的现成解决方案。我遇到了同样的情况,但 GridSplitter 不起作用,因此我创建了一个“ResizeBorder”控件,用于处理鼠标拖动其四个角以在二维中调整大小,或在两侧的中间拖动以在一维中调整大小。这实际上是非常简单的代码:只需处理 MouseDown,设置一个局部变量,给出 MouseDown 位置和被拖动的边/角,然后在 MouseMove 上更新大小。
我的 ResizeBorder 是可设计的,因此我可以在角落处显示四个框,在侧面显示线条,或者任何我能想到的更复杂的东西。
另请注意,无论您使用的是 Grid 和 GridSplitters 还是 ResizeBorder 或其他任何东西,您都可以选择将调整大小功能放在控件周围,如下所示:
或者通过更新 TextBox 本身的 ControlTemplate :
后一种方法的优点是您可以使用样式或附加属性来调整文本框的大小,并且可以轻松地在代码中动态更改文本框的大小可调整性。
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:
or by updating the ControlTemplate for TextBox itself:
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.