简体   繁体   English

如何在Xaml中控制按钮布局

[英]How to control button layout in Xaml

Im having trouble controlling the exact layout of a button control with XAML. 我无法使用XAML控制按钮控件的确切布局。 It seems that whatever i do the button is of a minimum width. 看来,无论我做什么按钮的最小宽度。 I have a simple button with only a textblock inside the button. 我有一个简单的按钮,按钮内只有一个文本块。 But the button has a lot of margin and padding that i cant seem to get rid of (i know of negative margins and padding). 但是按钮有很多空白和空白,我似乎无法摆脱(我知道空白和空白)。 The things i want to know is: 1. Why in the world was it designed this way. 我想知道的事情是:1.为什么在世界上如此设计。 2. what are the groundrules for controlling the exact layout of a button? 2.控制按钮的确切布局的基本规则是什么?

My code is as follows: 我的代码如下:

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

    <StackPanel Grid.Row="0"></StackPanel>

    <Pivot Grid.Row="1">
        <Pivot.Title>
            <StackPanel Orientation="Horizontal" Margin="-15,-3,0,0" Background="red" Width="480">
                <Button Background="Blue" x:Name="btnStudies" Click="btnMenuItem_Click" Width="20">
                    <TextBlock Text="Title" Foreground="White"></TextBlock>
                </Button>

                <Button Background="Green">
                    <TextBlock Text="Title" Foreground="White"></TextBlock>
                </Button>

                <Button Background="Blue" Click="btnMenuItem_Click">
                    <TextBlock Text="Title" Foreground="White"></TextBlock>
                </Button>

                <Button Background="Blue" Click="btnMenuItem_Click">
                    <TextBlock Text="Title" Foreground="White"></TextBlock>
                </Button>

                <Button Background="Blue" Click="btnMenuItem_Click">
                    <TextBlock Text="Title" Foreground="White"></TextBlock>
                </Button>
            </StackPanel>

        </Pivot.Title>
    </Pivot>
</Grid>

I want five buttons in a row but these are already too wide for the screen (windows phone). 我想要连续五个按钮,但这些按钮对于屏幕(Windows Phone)而言已经太宽了。 Changing the width doesnt seem to have any effect (why is it there). 更改宽度似乎没有任何效果(为什么在那里)。 The textBlock control within the button the button is as wide as the text on it, but i dont seem to have any control on the width of the button. 按钮内的textBlock控件与按钮上的文本一样宽,但是我似乎对按钮的宽度没有任何控件。 In HTML you only have padding or margin when you define it but in xaml it just seems to be there and for me its unclear how to undo that. 在HTML中,定义时只有填充或边距,但在xaml中似乎只是存在,对我而言,不清楚如何撤消该操作。

*****EDIT***** After reading Rachel's reply i decided to start from the ground up. *****编辑*****阅读雷切尔的回答后,我决定从头开始。 Using the code below i still have no control over how wide the button is because it uses a certain amount of padding that i cant seem to remove. 使用下面的代码,我仍然无法控制按钮的宽度,因为它使用了我似乎无法删除的一定数量的填充。 The button has a width of about 110 when i define a width lower than that it doesnt change. 当我定义的宽度小于其不变时,按钮的宽度约为110。 Margins and paddings of 0 have no effect at all (dont want to use negative values just yet because that doesnt seem very intuitive). 边距和填充为0根本没有效果(请不要使用负值,因为这似乎不是很直观)。 So the code below is very simple but still the button takes up an amount of space that i dont have any control over. 因此,下面的代码非常简单,但是按钮仍然占用了我无法控制的空间。 I cant imagine a reason why it was designed this way. 我无法想象采用这种方式设计的原因。

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="400" />
    </Grid.ColumnDefinitions>

    <StackPanel Width="300" Background="Red" HorizontalAlignment="Left">
        <Button Background="Blue" HorizontalAlignment="Left" Width="100" Margin="0" Padding="0">
            <TextBlock Text="Title" Width="Auto" HorizontalAlignment="Left" />
        </Button>
    </StackPanel>
</Grid>

The type and size of the parent panel containing the control affects the size/layout of the child controls. 包含控件的父面板的类型和大小会影响子控件的大小/布局。

In your case, you have a Grid as your parent panel, and a Grid defaults to taking up all available space. 在您的情况下,您将Grid作为父面板,并且Grid默认情况下会占用所有可用空间。 In addition, children placed inside the grid default to taking up all available space as well unless you specify otherwise. 此外,除非另行指定,否则放置在网格内的子级也会默认占用所有可用空间。

So your <Pivot> is being assigned a width equal to Grid.Width , and Pivot.Title sounds like it's being assigned a width equal to Pivot.Width , and StackPanel is being assigned a width equal to Pivot.Title.Width ... you get the picture. 因此,您为<Pivot>分配了等于Grid.Width的宽度,而Pivot.Title听起来像是被分配了等于Pivot.Width的宽度,而StackPanel被分配了等于Pivot.Title.Width的宽度...你明白了。

To specify that a control should not take up all available space, specify a HorizontalAlignment or VerticalAlignment property to tell it what side of the parent panel to dock the item on. 若要指定控件不应该占用所有可用空间,请指定HorizontalAlignmentVerticalAlignment属性,以告知控件将其停靠在父面板的哪一侧。

For example 例如

<Pivot Grid.Row="1" HorizontalAlignment="Left">

or 要么

<StackPanel OWidth="480" HorizontalAlignment="Left" ...>

If you're new to WPF's layout system, I would recommend reading through the codeproject article WPF Layouts: A Quick Visual Start to quickly learn what the main layout panels are for WPF. 如果您不熟悉WPF的布局系统,我建议您通读代码项目文章WPF布局:快速可视化入门,以快速了解WPF的主要布局面板。

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

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