大多數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