简体   繁体   English

如何将GroupBox,DataGrid和菜单置于位置

[英]How to postion GroupBox ,DataGrid, and Menu

Im struggling to position a GroupBox, a DataGrid and a Menu from Top to Bottom. 我在努力从顶部到底部放置一个GroupBox,一个DataGrid和一个菜单。 So for the first approach I used the StackPanel (Pseudo XAML Code): 因此,对于第一种方法,我使用了StackPanel(伪XAML代码):

<StackPanel>
    <GroupBox/>
    <DataGrid/>
    <Menu/>
</StackPanel>

The problem is that I want the DataGrid to fill the rest of the space between the Groupbox and Menu and it keeps displaying like that: 问题是我希望DataGrid填充Groupbox和Menu之间的其余空间,并一直这样显示: 在此处输入图片说明

I tried using the DockPanel: 我尝试使用DockPanel:

<DockPanel>
   <GroupBox DockPanel.Dock = "Top"/>
   <DataGrid/>
   <Menu DockPanel.Dock = "Bottom"/>
</DockPanel>

It stretches the DataGrid well but the Menu is not positioned properly 它很好地拉伸了DataGrid,但菜单放置不正确 在此处输入图片说明

Also in this case the DataGrid collapses after compilation 同样在这种情况下,DataGrid在编译后会崩溃
在此处输入图片说明

How should I do this so the elements display properly ? 我应该如何做才能使元素正确显示?

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="auto" />
    </Grid.RowDefinitions>

    <GroupBox Grid.Row="0" />
    <DataGrid Grid.Row="1" />
    <Menu Grid.Row="2" />
</Grid>

This will work - can't really give you a correct answer to why it works like this, but it has something to do with which components get created first whenever you are using docking. 这将行之有效-不能真正为您提供为什么它如此工作的正确答案,但是它与每次使用对接时首先创建组件有关。 So by placing menu above the datagrid, your dockpanel works as you described. 因此,通过在数据网格上方放置菜单,您的面板可以按照您所描述的进行工作。

Please someone correct me if i'm mistaking. 如果我误会,请有人纠正我。 Still learning about this myself. 自己仍在学习。

<DockPanel>
    <GroupBox Header="Filter" DockPanel.Dock="Top">
        <StackPanel Orientation="Horizontal">
        <Label Content="Label 1" />
        <Label Content="Label 2" />
        </StackPanel>
    </GroupBox>
    <Menu DockPanel.Dock="Bottom">
        <MenuItem Header="Menu 1">

        </MenuItem>
        <MenuItem Header="Menu 2">

        </MenuItem>
    </Menu>
    <DataGrid>
        <DataGrid.Columns>
            <DataGridTextColumn Header="Header1" Width="*" />
            <DataGridTextColumn Header="Header2" Width="*" />
            <DataGridTextColumn Header="Header3" Width="*" />
            <DataGridTextColumn Header="Header4" Width="*" />
        </DataGrid.Columns>
    </DataGrid>
</DockPanel>

在此处输入图片说明

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM