【问题标题】:Having multiple controls in one WPF grid cell在一个 WPF 网格单元格中有多个控件
【发布时间】:2012-04-25 07:02:02
【问题描述】:

WPF 新手,在线学习教程,有几个问题:

(1) 我试图让多个不同宽度的按钮并排显示在一个 WPF 网格单元中。然而,它们似乎总是相互叠加。我错过了什么?

(2) 是否可以控制网格单元内每个按钮的绝对起始左侧位置?

谢谢。

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<ScrollViewer>
    <Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          ShowGridLines ="True" >
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="60*" />
        </Grid.ColumnDefinitions>

        <Button Content="Button No. 1" Grid.Row="0" Grid.Column="0" />

        <GridSplitter HorizontalAlignment="Center" Width="6"
                      Grid.Row="0" Grid.Column="1" Grid.RowSpan="3" />

        <Button Content="Button No. 4-2" Grid.Row="0" Grid.Column="2" Width="100" />
        <Button Content="Button No. 4-1" Grid.Row="0" Grid.Column="2" Width="50" />

    </Grid>
</ScrollViewer>
</Page>

根据萨尔瓦多的回答,这是可行的:

        <Button VerticalAlignment="Top" HorizontalAlignment="Left" Content="Button No. 1" Grid.Row="0" Grid.Column="0" Height="100"/>

        <GridSplitter HorizontalAlignment="Center" Width="6"
                      Grid.Row="0" Grid.Column="1" Grid.RowSpan="3" />

        <Button VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0" Content="Button No. 4-2" Grid.Row="0" Grid.Column="2" Height="100" Width="100" />
        <Button VerticalAlignment="Top" HorizontalAlignment="Left" Margin="400,0,0,0" Content="Button No. 4-1" Grid.Row="0" Grid.Column="2" Height="150" Width="50" />

谢谢!

【问题讨论】:

    标签: wpf grid cell


    【解决方案1】:

    您可能希望在网格位置(行号 + 列号)中使用Container Controls(如CanvasDockPanelStackPanel)之一,然后将控件放入其中。

    这使得在一个网格位置布局多个控件变得更加容易。

    【讨论】:

      【解决方案2】:

      您没有设置VerticalAlignmentHorizontalAlignment 属性,因此默认情况下它们居中。

      您需要设置这些属性并将它们与 wpf 元素的 Margin 属性结合使用。

      看看这个Introduction to WPF Layout

      【讨论】:

        猜你喜欢
        • 2021-05-22
        • 1970-01-01
        • 2014-04-07
        • 2015-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-12
        • 1970-01-01
        相关资源
        最近更新 更多