对齐和使用XAML拉伸WPF按钮 [英] Align & Stretch WPF Buttons Using XAML

查看:162
本文介绍了对齐和使用XAML拉伸WPF按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在3列的网格中有3个WPF按钮.我希望每个按钮都能伸展以填充每个单元格的内容,但只能达到最大宽度.达到该宽度后,我想根据其网格列对齐每个按钮(左列按钮对齐到左侧,中间列按钮获取居中,右列按钮获取到右侧).这是到目前为止我无法使用的XAML:

I have 3 WPF buttons in a grid with 3 columns. I want each button to stretch to fill the contents of each cell, but only to a maximum width. Once that width has been achieved, I would like to then align each button according to it''s grid column (left column button gets aligned to the left, middle column button gets centered, right column button gets aligned to the right). Here''s the XAML I''ve go so far that isn''t working:

<Grid HorizontalAlignment="Stretch">
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Button HorizontalAlignment="Left"

        Width="Auto" Grid.Column="0" MaxWidth="100">Overwrite</Button>
    <Button HorizontalAlignment="Center"

        Width="Auto" Grid.Column="1" MaxWidth="100">Skip</Button>
    <Button HorizontalAlignment="Right"

        Width="Auto" Grid.Column="2" MaxWidth="100">Cancel</Button>
</Grid>

这只会使按钮的大小与其内容(而不是可用空间)大小有关,但会正确对齐它们.如果您在Kaxaml中检查上述XAML,您将明白我的意思.我要实现的目标是使这些按钮正确对齐并增长到100个单位的宽度.根据我到目前为止所看到的,调整大小和对齐似乎与WPF中的紧密联系无法实现.

That just makes the buttons size to their content (rather than to available space), but it aligns them properly. If you inspect the above XAML in Kaxaml, you''ll see what I mean. What I''m trying to achieve is having these buttons align properly AND grow up to 100 units wide. From what I''ve seen so far, resizing and aligning appear to be too closely linked in WPF to achieve this.

关于如何将按钮拉伸到最大宽度并在达到最大宽度后水平对齐的任何想法?

推荐答案

如何添加另外两个虚拟"列?将按钮放在第1、3和5列中,并将该列的宽度设置为自动",并将最大尺寸设置为100(而不是该按钮).对于第2列和第4列,将宽度设置为*.
How about adding two more "dummy" columns? Put your buttons in columns 1,3 and 5, and set the column''s width to Auto and maximum size to 100 (not the button''s). And for columns 2 and 4 set the width to *.


天才!不知道为什么我没有想到这一点.这是我(读到:您)想出的(并且效果很好):
You''re a flippin'' genius! Don''t know why I didn''t think of that. Here''s what I (read: you) came up with (and it works reasonably well):
<Grid HorizontalAlignment="Stretch">
	<Grid.ColumnDefinitions>
		<ColumnDefinition MaxWidth="100" Width="*" />
		<ColumnDefinition Width="0.01*" />
		<ColumnDefinition MaxWidth="100" Width="*" />
		<ColumnDefinition Width="0.01*" />
		<ColumnDefinition MaxWidth="100" Width="*" />
	</Grid.ColumnDefinitions>
	<Button Grid.Column="0">b1</Button>
	<Button Grid.Column="2">b2</Button>
	<Button Grid.Column="4">b3</Button>
</Grid>


我必须将其他列的宽度设置为较小的比例,因为自动"会导致网格折叠这些列,而值"1 *"会导致按钮过早收缩.再次感谢,好主意!


I had to set the width of the other columns to some small proportion because "Auto" was causing the grid to collapse those columns and a value of "1*" was causing the buttons to shrink prematurely. Thanks again, great idea!


这篇关于对齐和使用XAML拉伸WPF按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆