简体   繁体   English

如何使控件填充WPF中的容器?

[英]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 : 所以,我的意思是填充容器是:

  1. Making lbxBrowse to fill the mid-space of the DockPanel inside the left StackPanel . 使lbxBrowse填充左侧StackPanelDockPanel的中间空间。
  2. Making txtKeyword to fill the WrapPanel . 使txtKeyword填充WrapPanel
  3. Making stbBottom to fill the WrapPanel . 使stbBottom填充WrapPanel

What I've tried : 我尝试过的

  1. It seems i've put them in a DockPanel with LastChildFill="True" . 看来我已经将它们放在带有LastChildFill="True"DockPanel But it seems doesn't work. 但这似乎行不通。
  2. I don't know about this, since it's not possible to put it into a DockPanel first. 我对此一无所知,因为不可能先将其放入DockPanel
  3. I don't know anything about this. 我对此一无所知。

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. ToolBarStatusBar上的固定大小似乎是必需的,否则,将看不到文本。

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 . 代替使用StackPanelDockPanel ,可以使用Grid并设置Grid.ColumnDefinitionsGrid.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.

相关问题 如何使StackPanel用WPF中的高度填充容器 - How to make StackPanel to fill his container with and height in WPF 当 Horizo​​ntalAlignment 和 Horizo​​ntalContentAlignment 不起作用时,如何使 WPF 中的控件拉伸以填充可用宽度? - How can I make controls in WPF stretch to fill available width when HorizontalAlignment and HorizontalContentAlignment don't work? 在 C# 中创建时,如何使 System.Windows.Forms.Panel 填充 WPF 中的容器? - How to make System.Windows.Forms.Panel fill the container in WPF when created in C#? 如何使tekla控件在WPF中工作? - how to make tekla controls work inside WPF? 如何更快地在 WPF 应用程序中添加控件 - How to make adding controls in a WPF application faster 如何在WPF中使用DockStyle.Fill作为标准控件? - How to use DockStyle.Fill for standard controls in WPF? WPF FlowDocument将不会填充Viewbox容器 - WPF FlowDocument will not fill Viewbox container 调整列表中控件的大小以填充可用空间WPF - Resize controls in a list to fill the available space WPF 如何结合使用WPF控件使其看起来更好。 怎么样? - How to combine WPF Controls to make it look good. How? 如何让叙述者阅读 wpf 中堆栈面板内的控件 - how to make narrator to read the controls inside stackpanel in wpf
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM