wpf - 在一个 WPF 网格单元中有多个控件

标签 wpf grid cell

刚接触 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" />

谢谢!

最佳答案

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

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

看看这个Introduction to WPF Layout

关于wpf - 在一个 WPF 网格单元中有多个控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10131944/

相关文章:

wpf - WPF中MDI的替代品?

itext - 将 itextsharp 条形码图像添加到 PdfPCell

html - 响应式 div 视口(viewport)大小的 50% 高度

excel - 镜像片材之间的单元格范围

matlab - 快速列子集选择

wpf - 在 Prism/Composite MVVM WPF 应用程序中使 View 模型激活感知

wpf - 将 GridViewColumn 中的复选框居中?

c# - 需要一个长期运行的模式或解决方案,该模式或解决方案需要循环执行并在每个线程完成后更新UI

html - 如何让图像贴在 div 的底部并与它上面的 div 重叠

matlab - 在Matlab中求每个网格的权重?