可调整大小的多个文本框 [英] Resizeable multiple TextBoxes
问题描述
我将创建一些程序,该程序可调整大小(通过拖动)多个文本框。
I'm going to create some program that resizeable (with drag) multiple Textboxes.
但是,我不知道如何构建此布局。
But, I don't know how to build this layout. Is there know how to create drag layout?
推荐答案
目前尚不清楚您的确切说明是什么。但是该图形使您看起来像希望网格中的一些单元格具有不同宽度的抓手,而其他单元格则没有。为此,您应该能够使用 GridSplitter
对象。
It's not entirely clear what your exact specification is here. But the drawing makes it look like you want some cells within the grid to have grab handles for varying width, while others do not. For this purpose, you should be able to use the GridSplitter
object.
例如:
<Window x:Class="TestSO36334781GridSplitter.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:p="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<Border BorderBrush="Black" BorderThickness="1">
<Grid>
<Grid.Resources>
<p:Style TargetType="GridSplitter">
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="Width" Value="5"/>
<Setter Property="Height" Value="10"/>
<Setter Property="Background" Value="Black"/>
<!-- Offset the splitter visually so it's centered over the gridline -->
<Setter Property="RenderTransform">
<Setter.Value>
<TranslateTransform X="2.5" Y="0"/>
</Setter.Value>
</Setter>
</p:Style>
<p:Style TargetType="TextBox">
<Setter Property="Height" Value="30"/>
</p:Style>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="Label1" Grid.Column="0"/>
<TextBlock Text="Label1" Grid.Column="1"/>
<TextBlock Text="Label1" Grid.Column="2"/>
<TextBox Grid.Row="1" Grid.Column="0"/>
<TextBox Grid.Row="1" Grid.Column="1"/>
<TextBox Grid.Row="1" Grid.Column="2"/>
<GridSplitter Grid.Row="1" Grid.Column="0"/>
<GridSplitter Grid.Row="1" Grid.Column="1"/>
<TextBox Grid.Row="2" Grid.ColumnSpan="3" Text="A wide textbox here"/>
</Grid>
</Border>
</StackPanel>
</Window>
上面显示的是带有三个 TextBox
的网格中间行中的控件,用户可以通过在每个控件之间拖动 GridSplitter
来更改控件的宽度。它们上方的标签(即 TextBlock
对象)也被移动/调整大小,因为它们与每个 TextBox $ c共享同一列$ c>。
The above shows a grid with three TextBox
controls in the middle row, the widths of which can be modified by the user by dragging the GridSplitter
between each of them. The labels above them (i.e. the TextBlock
objects) are moved/resized as well, as they share the same column with each respective TextBox
.
显示第四个 TextBox
,跨越最后一行的三列,以显示您如何仍然可以具有独立于拆分器的其他网格元素。我假设您可以修改基本概念以适合您的特定需求。
A fourth TextBox
is shown, spanning three columns in the last row, to show how you can still have other grid elements independent of the splitters. I assume you can modify the basic idea to suit your specific needs.
请注意,重要的是为分离器对象提供特定的格式,并且它们会显示为在与它们共享网格元素的控件之后,因此它们在z顺序中位于这些控件的上方。
Note that it's important you provide your specific formatting for the splitter objects, and that they appear after the controls they share grid elements with, so that they are above those controls in the z-order.
另请参见以下堆栈溢出问题: WPF用户控制的网格列宽度
See also this Stack Overflow question: WPF user controlled grid column width
附录:
Joey ,可以放置拆分器控件,而不必与网格中的其他元素共享单元格(并且可能使网格模糊)。以下XAML代码段(即仅 Grid
元素)显示了其工作方式:
As hinted at in the (now deleted) comments by Joey, it is possible to place splitter controls without them having to share the cell with (and possibly obscuring) other elements in the grid. The following XAML snippet (i.e. just the Grid
element) shows how that would work:
<Grid>
<Grid.Resources>
<p:Style TargetType="GridSplitter">
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="Width" Value="5"/>
<Setter Property="Height" Value="10"/>
<Setter Property="Background" Value="Black"/>
</p:Style>
<p:Style TargetType="TextBox">
<Setter Property="Height" Value="30"/>
</p:Style>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="Label1" Grid.Column="0"/>
<TextBlock Text="Label1" Grid.Column="2"/>
<TextBlock Text="Label1" Grid.Column="4"/>
<TextBox Grid.Row="1" Grid.Column="0"/>
<TextBox Grid.Row="1" Grid.Column="2"/>
<TextBox Grid.Row="1" Grid.Column="4"/>
<GridSplitter Grid.Row="1" Grid.Column="1" ResizeBehavior="PreviousAndNext"/>
<GridSplitter Grid.Row="1" Grid.Column="3" ResizeBehavior="PreviousAndNext"/>
<TextBox Grid.Row="2" Grid.ColumnSpan="5" Text="A wide textbox here"/>
</Grid>
以上内容消除了 RenderTransform
的需要,因为每个 GridSplitter
都以其自己的列为中心。 ResizeBehavior
设置为 PreviousAndNext
,因此拖动拆分器不会影响拆分器所在的列的宽度
The above eliminates the need for the RenderTransform
, as each GridSplitter
winds up centered in its own column. The ResizeBehavior
is set to PreviousAndNext
, so that dragging the splitter affects not the width of the column in which the splitter is contained, but instead the widths of the columns immediately before and after it.
可能包含但不包含其前后的列宽。您可能可以应用 DataGrid
控件,并使其执行您想要的操作。但是,您的问题中没有任何东西暗示我需要 DataGrid
的完整功能集,甚至不需要满足某些约束条件(例如标题的格式设置,以及是否可以在布局中包含其他固定宽度的元素。
It's possible that you could apply a DataGrid
control in this scenario and get it to do what you want. But there's nothing in your question that suggests to me you need the full feature set of a DataGrid
, or even that you'll be happy with some of the constraints that would involve (such as the way headings are formatted, and whether you can include other fixed-width elements in the layout).
这篇关于可调整大小的多个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!