在WPF调整大小文本框在运行时 [英] Resizing TextBox At Runtime in WPF

查看:520
本文介绍了在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?

感谢您:)

推荐答案

tehMick的答案是绝对正确的:你一定要创建一个容器做调整,而不是自定义文本框本身。而如果你的作品 GridSplitter 是一个很好的,现成的解决方案。

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.

我有同样的情况,但GridSplitter是行不通的,所以我创建了一个ResizeBorder控件处理鼠标拖动它的四个角来调整在二维或两侧的中间的一个调整。这其实是很简单的code:刚刚处理的MouseDown,设置给予的MouseDown位置和侧面/拐角处的局部变量被拖动,然后在的MouseMove更新大小

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.

我的ResizeBorder是风格化的,所以我可以只显示四个框在弯道和线两侧,或任何更复杂,我可以梦想。

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.

另外请注意,无论您使用的是网格和GridSplitters或ResizeBorder或其他任何东西,你必须把你的大小调整功能的选择的或者的像这样的控件周围:

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}" ... />

这后一种方法的优点是,你可以使用样式或附加属性,使文本框可以调整大小,并可以很容易地改变文本框的大小可调整动态的code。

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.

这篇关于在WPF调整大小文本框在运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆