简体   繁体   English

在WPF中运行时调整TextBox的大小

[英]Resizing TextBox At Runtime in WPF

Was just wondering how I would go about letting the user resize a TextBox control at runtime by dragging its corners in WPF. 只是想知道如何通过在WPF中拖动角来让用户在运行时调整TextBox控件的大小。 Less importantly, is the same technique used for the resizing of all controls? 不太重要的是,用于调整所有控件大小的技术是否相同?

Thank you :) 谢谢 :)

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>

tehMick's answer is absolutely correct: You should definitely create a container to do the resizing rather than customizing the TextBox itself. tehMick的答案是绝对正确的:你绝对应该创建一个容器来进行调整大小而不是自定义TextBox本身。 And if it works for you GridSplitter is a very good in-the-box solution. 如果它适用于你GridSplitter是一个非常好的开箱即用解决方案。

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. 我有相同的情况,但GridSplitter无法工作,所以我创建了一个“ResizeBorder”控件,它在四个角上处理鼠标拖动以在两个维度上调整大小,或者在两个中间调整大小。 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. 这实际上是非常简单的代码:只需处理MouseDown,设置一个局部变量,给出MouseDown位置和侧面/角落被拖动,然后在MouseMove上更新大小。

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. 我的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: 另外请注意,无论您使用的是电网和GridSplitters或ResizeBorder或其他任何东西,你必须把你的调整大小功能两种这样的控件周围的选择:

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

or by updating the ControlTemplate for TextBox itself: 或者通过更新TextBox本身的ControlTemplate:

<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. 后一种方法的优点是您可以使用样式或附加属性来使TextBoxes可以调整大小,并且您可以在代码中动态地更改TextBox的可调整性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM