WPF按钮相同/推荐宽度 [英] WPF buttons same/recommended width

查看:530
本文介绍了WPF按钮相同/推荐宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有一个带有多个按钮的窗口,如Ok / Cancel或Yes / No / Cancel。所有按钮的宽度必须相同。显然,这可以通过猜测一个数字和硬连线来完成这个数字。



有没有更好的方法来做,一个会考虑到首选/推荐的大小(只要Ok按钮有多宽?这不是一个修辞问题,实际上不知道答案!),最长字幕的文本需要什么,如果字体大小增加会发生什么等等?

解决方案

有几种方法可以做到这一点:



1)使用网格进行布局。每个按钮都有自己的列,它是星号。这样,所有列的大小相同:

 < Grid> 
< Grid.ColumnDefinitions>
< ColumnDefinition Width =*/>
< ColumnDefinition Width =*/>
< ColumnDefinition Width =*/>
< /Grid.ColumnDefinitions>
< Button Grid.Column =0>是< / Button>
< Button Grid.Column =1>否< / Button>
< Button Grid.Column =2>取消< / Button>
< / Grid>

2)您可以有一个项目为主大小,并将所有其他项目的宽度绑定到此项目的宽度。

 < StackPanel Orientation =Horizo​​ntal> 
< Button Name =MasterButtonWidth =100>是< / Button>
< Button>
< Button.Width>
< Binding ElementName =MasterButtonPath =Width/>
< /Button.Width>

< / Button>
< / StackPanel>

编辑:在实际代码中,您可能会有Width =Auto 。由于其他宽度基于主宽度,所以应选择最宽的按钮(最宽的文本)。


Suppose you have a window with multiple buttons such as Ok/Cancel or Yes/No/Cancel. All the buttons need to be the same width. Obviously this could be done by just guessing a number and hardwiring all of them to that number.

Is there a better way to do it, one that would take into account preferred/recommended sizes (just how wide should an Ok button be anyway? This is not a rhetorical question, I actually don't know the answer!), what's needed by the text of the longest caption, what happens if the font size is increased etc?

解决方案

There are several ways to do this:

1) Use a Grid for layout. Each Button gets its own Column, which is Star-sized. That way, all columns are the same size:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Button Grid.Column="0">Yes</Button>
    <Button Grid.Column="1">No</Button>
    <Button Grid.Column="2">Cancel</Button>
</Grid>

2) You can have one item as "master size" and bind the width of all others to this item's width.

<StackPanel Orientation="Horizontal">
    <Button Name="MasterButton" Width="100">Yes</Button>
    <Button>
        <Button.Width>
            <Binding ElementName="MasterButton" Path="Width"/>
        </Button.Width>
        No
    </Button>
</StackPanel>

EDIT: In actual code, you probably will have Width="Auto". Since the other widths are based on the "master width", the button with the widest width (widest text) should be chosen.

这篇关于WPF按钮相同/推荐宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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