同步从 ItemsSource 生成的项目
我正在寻找一种简单的方法来同步由 ItemsControl 生成的文本大小(按钮内容)。
我正在使用以下 Xaml 代码:
<ItemsControl ItemsSource="{Binding UseCases}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding DisplayName}" Width="200" Height="200">
<Button.ContentTemplate>
<DataTemplate>
<Viewbox>
<ContentPresenter Content="{Binding}" />
</Viewbox>
</DataTemplate>
</Button.ContentTemplate>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我想让按钮上的文本尽可能大,这已经可以工作了。
但由于文字长度不同,每个按钮的文字大小也不同,看起来很奇怪。
有没有一种简单的方法来告诉 Viewbox(或任何其他方式)采用最小文本的大小并将其用于每个按钮?
I'm looking for an easy way to synchronize Text sizes (Button Content), which are generated by an ItemsControl.
I'm using the following Xaml code:
<ItemsControl ItemsSource="{Binding UseCases}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding DisplayName}" Width="200" Height="200">
<Button.ContentTemplate>
<DataTemplate>
<Viewbox>
<ContentPresenter Content="{Binding}" />
</Viewbox>
</DataTemplate>
</Button.ContentTemplate>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I want to make the text on the buttons as big as possible, which already works.
But since the text length is different, the text sizes for each button is also different, which looks odd.
Is there a simple way to tell the Viewbox (or any other way) to take the size of the smallest text and use it for every button?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要限制文本的大小,只需使用 Padding:
处理文本溢出是复杂的部分。由于某种原因(罪魁祸首是 - ButtonChrome),按钮不会采用“TextBlock.TextTrimmingProperty”附加属性,但是 AttachedProperty 机制是专门为此类情况设计的),给您留下两个选择:
覆盖按钮的模板,查找 ButtonChrome,删除它并替换为具有 TextBlock 的内容,将该 TextBlock 的文本绑定到 ContentControl.Content。
自行管理文本溢出。 Sibscribe for SizeChanged 事件,从事件参数获取大小(它可能在其他地方不可用),获取填充并确定文本是否超出可用大小。将多余的部分替换为“..”。
道德——不值得做。
我会创建统一的二次启动按钮并在它们旁边放置标签。
to limit the size of text just use Padding:
Handling text overflow is the complex part. By some reason (the culprit is - ButtonChrome) the button won't take 'TextBlock.TextTrimmingProperty' attached property, however the AttachedProperty mechanism is designed specifically for the cases like that), leaving you with two options:
Override button's template, lookup for ButtonChrome, get rid of it and replace with something, which has a TextBlock, bind that TextBlock's text to ContentControl.Content.
Manage your text overflow by yourself. Sibscribe for SizeChanged event, get size from the event argument (it mightn't be available anywhere else), get the padding and figure out if text exceeds the available size. Replace the excessive part of it with "..".
The moral - not worth doing.
I'd create uniform quadratic launch buttons and put labels beside them.