根据最大按钮的内容均匀大小的按钮 [英] Evenly-sized buttons according to content of largest button

查看:29
本文介绍了根据最大按钮的内容均匀大小的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在一个窗口上有两个 WPF 按钮,内容如下:

<按钮>取消</按钮>

我希望这些按钮的宽度相同,但是在 Content 绑定到给定用户语言的本地化值的情况下,我不知道按钮需要多宽是为了适应新的内容.

我如何为这些按钮应用最小宽度,以便将最宽的宽度(根据内容)有效地用作两者的 MinWidth,从而使它们保持一致?

或者换一种说法:我不希望按钮是它们容器的宽度(除非以巧妙的方式使用容器是我问题的答案),而且我不希望它们每个都只需根据自己的内容调整大小,因为这将使它们的大小不同.我想要介于两者之间的东西.具有最大内容大小的内容以显示其内容,而所有其他内容大小相同的宽度,因此宽度都相等.

我希望答案在于将它们放入某种容器中.我知道我可以使用 Grid 并让它们填充网格单元格",但关键是我也不希望它们 宽.我知道我可以在按钮的 Content_Changed 事件上运行一些代码隐藏,并将最小宽度设置为最宽按钮的最小宽度,但我对纯 xaml 方法感兴趣.我可能需要创建一个扩展 ItemsControl 的自定义控件,该控件在添加新项目或重新调整大小时隐藏代码并将最宽项目的宽度应用为 MinWidth 所有其他项目.

非常感谢.

解决方案

Use UniformGrid<UniformGrid Horizo​​ntalAlignment="Right" Rows="1" Columns="2"><按钮内容="确定" Grid.Column="0"/><按钮内容="取消" Grid.Column="1"/></UniformGrid>

Imagine I have two WPF buttons on a window, with content as follows:

<Button>OK</Button>
<Button>Cancel</Button>

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.

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.

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.

Many thanks in advance.

解决方案

Use UniformGrid

<UniformGrid HorizontalAlignment="Right" Rows="1" Columns="2">
  <Button Content="Ok" Grid.Column="0"/>
  <Button Content="Cancel" Grid.Column="1"/>
</UniformGrid>

这篇关于根据最大按钮的内容均匀大小的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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