WPF按钮相同/推荐宽度 [英] WPF buttons same/recommended width
问题描述
有没有更好的方法来做,一个会考虑到首选/推荐的大小(只要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 =Horizontal>
< 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屋!