大多数WPF控件(因此用户控件)的默认行为是扩大并占据可用
例如(省略掉了清晰XML命名空间)尽可能多的空间
<Window>
<Button Content="Hello" />
</Window>
将创建一个大按钮,如果你最大化你的窗口
在你的情况,我猜你正在使用Visual Studio设计器来调整和调整个别控件。设计者生成的代码的一种行为是如果您将控件移到位置,则设置Margin
属性,如果调整大小,则将属性设置为Width
和Height
。这导致他们停止扩展或拉伸。
通常,我避免使用可视化设计器,而是直接使用XAML编辑器来设计布局,并验证其在视觉设计器上的显示效果。
虽然根据屏幕可用性控件扩展和缩小是很好的,但它有时可能会导致较差或较差的布局和大小。通常,当我使用Grid
作为布局时,我为个别子节点MinWidth
,MinHeight
,MaxWidth
和MaxHeight
设置了拉伸或压缩过多时不太好看的控件(RadioButtons,CheckBox)。对于某些控件,只需水平拉伸(对于您的案例中的问题定义字段,单行文本输入即可),将Grid
行高设置为auto
以便控件达到所需的高度就足够了。
这也可能是OK使用具有合理的最小/最大宽度约束GridSplitter
允许用户调整LHS列,如果他的窗口尺寸太小
像这样的东西(未测试虽然)
<Window>
<Window.Resources>
<Style TargetType="TextBox">
<Setter Property="Margin" Value="5" />
</Style>
<Style TargetType="Button">
<Setter Property="Margin" Value="5" />
</Style>
<Style TargetType="TextBlock">
<Setter Property="Margin" Value="5" />
<Setter Property="FontWeight" Value="Bold"/>
</Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.3*" MinWidth="200" MaxWidth="350" />
<ColumnDefinition Width="0.7*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<!-- first column -->
<TextBlock Grid.Column="0"
Grid.Row="0"
Text="Employee ID" />
<TextBox Grid.Column="0"
Grid.Row="1"
Text="{Binding EmployeeID}" />
<TextBlock Grid.Column="0"
Grid.Row="2"
Text="User Name" />
<TextBox Grid.Column="0"
Grid.Row="3"
Text="{Binding UseName}" />
<TextBlock Grid.Column="0"
Grid.Row="4"
Text="Computer Name" />
<TextBox Grid.Column="0"
Grid.Row="5"
Text="{Binding ComputerName}" />
<TextBlock Grid.Column="0"
Grid.Row="6"
Text="PhoneNumber" />
<TextBox Grid.Column="0"
Grid.Row="7"
Text="{Binding PhoneNumber}" />
<TextBlock Grid.Column="0"
Grid.Row="8"
Text="Location" />
<TextBox Grid.Column="0"
Grid.Row="9"
Grid.RowSpan="2"
Text="{Binding Location}" />
<Button Grid.Column="0"
Grid.Row="11"
Text="Copy All" />
<!-- second column -->
<TextBlock Grid.Column="1"
Grid.Row="0"
Text="Problem Description" />
<Grid Grid.Column="1"
Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0"
Text="{Binding ProblemDescription}" />
<Button Grid.Column="1"
Content="C" />
</Grid>
<TextBlock Grid.Column="1"
Grid.Row="2"
Text="Notes" />
<TextBox Grid.Column="0"
Grid.Row="3"
Grid.RowSpan="7"
Text="{Binding Notes}" />
<TextBlock Grid.Column="1"
Grid.Row="10"
Text="Resolution" />
<Grid Grid.Column="1"
Grid.Row="11">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0"
Text="{Binding Resolution}" />
<Button Grid.Column="1"
Content="C" />
</Grid>
<!-- splitter to make the columns resizable -->
<GridSplitter Grid.Column="1"
Grid.Row="0"
Grid.RowSpan="12"
Width="3"
Background="Blue" />
</Grid>
</Window>
是的,我会给这个镜头。我试图避免使用列和行的网格。然而,在阅读你所做的并做了一些研究之后,看起来这可能是做我想做的事情的唯一方法。 – Ben