首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在一个WPF网格单元格中具有多个控件

在一个WPF网格单元格中具有多个控件
EN

Stack Overflow用户
提问于 2012-04-13 05:13:25
回答 2查看 23.3K关注 0票数 11

WPF新手入门并通过在线教程学习,有几个问题:

(1)我正在尝试让多个不同宽度的按钮并排出现在一个WPF网格单元格中。然而,它们似乎总是堆叠在一起。我错过了什么?

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

谢谢。

代码语言:javascript
复制
<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>

基于来自萨多瓦的答案,这是可行的:

代码语言:javascript
复制
        <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" />

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-13 05:19:57

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

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

看一下这个Introduction to WPF Layout

票数 11
EN

Stack Overflow用户

发布于 2012-04-13 05:19:10

您可能希望在网格位置(Row #+ Column #)使用其中一个Container Controls (如CanvasDockPanelStackPanel),然后将控件放入其中。

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

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10131944

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档