[英]How to make controls fill the container in WPF?
Below is my xaml code : 以下是我的xaml代码:
<Window x:Class="ScoresBank.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<DockPanel LastChildFill="True">
<WrapPanel DockPanel.Dock="Top">
<ToolBar Height="25"></ToolBar>
</WrapPanel>
<WrapPanel DockPanel.Dock="Bottom">
<StatusBar Name="stbBottom" Height="25"
BorderThickness="2"
BorderBrush="Black">Example</StatusBar>
</WrapPanel>
<StackPanel DockPanel.Dock="Left">
<DockPanel LastChildFill="True">
<WrapPanel DockPanel.Dock="Top">
<Button Name="btnBrowseTags">TAGS</Button>
<Button Name="btnBrowseTitles">TITLES</Button>
</WrapPanel>
<ComboBox DockPanel.Dock="Top" Name="cbxCategory">
Example
</ComboBox>
<WrapPanel DockPanel.Dock="Bottom">
<TextBox Name="txtKeyword"></TextBox>
<Button Name="btnFind">Find</Button>
</WrapPanel>
<ListBox Name="lbxBrowse">
</ListBox>
</DockPanel>
</StackPanel>
<StackPanel DockPanel.Dock="Right">
<Button>Example</Button>
</StackPanel>
<Canvas>
</Canvas>
</DockPanel>
And this is my current layout view : 这是我当前的布局视图:
So, what I mean with filling the container are : 所以,我的意思是填充容器是:
lbxBrowse
to fill the mid-space of the DockPanel
inside the left StackPanel
. lbxBrowse
填充左侧StackPanel
内DockPanel
的中间空间。 txtKeyword
to fill the WrapPanel
. txtKeyword
填充WrapPanel
。 stbBottom
to fill the WrapPanel
. stbBottom
填充WrapPanel
。 What I've tried : 我尝试过的
DockPanel
with LastChildFill="True"
. LastChildFill="True"
的DockPanel
。 But it seems doesn't work. DockPanel
first. DockPanel
。 I don't use fixed size, since I need them to keep neat even when resized in multiple screen resolution. 我不使用固定尺寸,因为即使在多屏幕分辨率下调整尺寸,我也需要它们保持整洁。 The fixed size on
ToolBar
and StatusBar
seems required, otherwise, the text will be unseen. ToolBar
和StatusBar
上的固定大小似乎是必需的,否则,将看不到文本。
PS If possible, I prefer the solution to be XAML code, rather than the C# code. PS如果可能的话,我更喜欢将解决方案作为XAML代码而不是C#代码。 Otherwise, C# code is fine too.
否则,C#代码也可以。
Thank you. 谢谢。
Instead of using a StackPanel
and DockPanel
you can use Grid
and set Grid.ColumnDefinitions
and Grid.RowDefinitions
. 代替使用
StackPanel
和DockPanel
,可以使用Grid
并设置Grid.ColumnDefinitions
和Grid.RowDefinitions
。 You can specify directly each row Height
and each column Width
. 您可以直接指定每一行的
Height
和每一列的Width
。 It's easier to use and it automaticly fit to content and container. 它更易于使用,并且自动适应内容和容器。
You should use a Grid
. 您应该使用
Grid
。 It is more easier to configure. 它更容易配置。 Here is an example (I don't know exactly how you want to setup your layout).
这是一个示例(我不完全知道您要如何设置布局)。
<Window x:Class="SampleWpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="400" Width="600">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Margin="5" Content="TAGS"
Grid.Column="0" Grid.Row="0" />
<Button Margin="5" Content="TITLES"
Grid.Column="1" Grid.Row="0" />
<Button Margin="5" Content="EXAMPLES"
Grid.Column="2" Grid.Row="0"
HorizontalAlignment="Right"/>
<ComboBox Margin="5"
Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" />
<ListBox Margin="5"
Grid.Column="2" Grid.Row="1" Grid.RowSpan="2" />
<Button Margin="5" Content="EXAMPLE"
Grid.Column="2" Grid.Row="3" HorizontalAlignment="Left" />
</Grid>
</Window>
And the result: 结果:
With a grid you can set the height and the width of the columns and rows to a specific value (in points, pixels, cm, etc.), to column content ( Auto
) or proportional to the grid (with *
). 使用网格,您可以将列和行的高度和宽度设置为特定值(以点,像素,cm等为单位),列内容(
Auto
)或与网格成比例(使用*
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.