根据最大按钮的内容,大小均匀的按钮 [英] Evenly-sized buttons according to content of largest button
问题描述
想象一下,我在一个窗口上有两个WPF按钮,其内容如下:
Imagine I have two WPF buttons on a window, with content as follows:
<Button>OK</Button>
<Button>Cancel</Button>
我希望这些按钮具有相同的宽度,但是在Content
绑定到给定用户语言的本地化值的情况下,我不知道按钮的宽度需要多大才能容纳新的内容.
I want these buttons to be the same width, however in a scenario where the Content
is bound to a localised value for a given user's language, I don't know how wide the buttons need to be to accommodate the new content.
如何对这些按钮应用最小宽度,以使最宽按钮的宽度(根据内容而定)有效地用作两个按钮的MinWidth
,从而使它们保持一致?
How might I apply a minimum width to these buttons, such that the width of the widest one (according to content) is effectively used as the MinWidth
of both, thus keeping them uniform?
或者换一种说法:我不希望按钮成为它们容器的宽度(除非以一种聪明的方式使用容器可以解决我的问题),而且我也不希望每个按钮都成为它们的容器调整大小以适合自己的内容,因为这会使它们具有不同的大小.我想要介于两者之间的东西.内容显示尺寸最大的一个显示内容,其他尺寸显示相同的宽度,因此宽度都相等.
Or to put it another way: I don't want the buttons to be the width of their container (unless using a container in a clever way is the answer to my problem), and I don't want them each to just size to their own content because that will make them different sizes. I want something in-between. The one with the largest content to size to display it's content, and all others to size to that same width, so the widths are all equal.
我希望答案在于将它们放入某种容器中.我知道我可以使用Grid
并让它们填充网格单元格",但是重点是我也不希望它们太宽.我知道我可以在按钮的Content_Changed
事件上运行一些代码隐藏,并将最小宽度设置为最宽按钮的最小宽度,但是我对纯xaml方法感兴趣.可能需要创建一个扩展ItemsControl
的自定义控件,当添加新项目或调整其大小时,该代码会在后面隐藏代码,并将最宽项目的宽度用作所有其他项目的MinWidth
.
I expect the answer lies in putting them in some sort of container. I know I could use a Grid
and let them fill grid "cells", but the point is I don't want them to be too wide, either. I know I could have some code-behind that runs on the Content_Changed
event of the buttons and sets the minwidth to that of the widest button, but I'm interested in a pure-xaml method. It might be I need to create a custom control extending ItemsControl
that rusn code-behind when new items are added or re-sized and applies the width of the widest item as the MinWidth
of all the other items.
非常感谢.
推荐答案
Use UniformGrid
<UniformGrid HorizontalAlignment="Right" Rows="1" Columns="2">
<Button Content="Ok" Grid.Column="0"/>
<Button Content="Cancel" Grid.Column="1"/>
</UniformGrid>
这篇关于根据最大按钮的内容,大小均匀的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!