首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ItemsControl拉伸FontSize of ButtonContent

ItemsControl拉伸FontSize of ButtonContent
EN

Stack Overflow用户
提问于 2016-04-24 17:09:42
回答 1查看 344关注 0票数 1

自动调整按钮(文本)内容的大小有问题。

这是我的扣子的样式。除了注意TextWrapping属性之外,没有什么特别的!

代码语言:javascript
复制
<Style TargetType="{x:Type Button}">
  <Setter Property="Template">
  <Setter.Value>
    <ControlTemplate TargetType="Button">
      <TextBlock Text="{TemplateBinding Content}" TextWrapping="Wrap"/>
    </Setter.Value>
  </Setter>
</Style>

我根据我的按钮定义了一个ItemTemplate:

代码语言:javascript
复制
<DataTemplate x:Key="MyItemTemplate">
  <Button Content="{Binding Content}" Command="{Binding Command}" FontWeight="Bold" FontSize="{Binding FontSize}"/>
</DataTemplate>

我显示了一个带有3列的ItemsControl的项目列表:

代码语言:javascript
复制
<ItemsControl ItemsSource="{Binding MyItems}" ItemTemplate="{StaticResource MyItemTemplate}">
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <UniformGrid Columns="3"/>
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
</ItemsControl>

在此之前,这没什么特别的。但是现在,我希望将我所有按钮的FontSize尽可能地拉长,内容最多的项目允许(所有项目最终都应该具有相同的FontSize )。

我找到了一个solution来计算所需的文本大小。所以我可以计算按钮的最大FontSize。因此,我需要我的一个按钮的实际大小(所有按钮都有相同的大小)。

我真的不知道如何得到大小和解决我的问题。如果有人有个主意或者一个完全不同的方法,那就太好了。

EN

回答 1

Stack Overflow用户

发布于 2016-04-24 19:55:12

您可以使用按钮的ActualWidth作为CommandParameter来获得如下宽度:

代码语言:javascript
复制
<UserControl ....
.................>
<i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <i:InvokeCommandAction Command="{Binding Command}" CommandParameter="{Binding ActualWidth,ElementName=button}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>

    <DataTemplate x:Key="MyItemTemplate">
                    <Button Content="{Binding Content}" x:Name="button"
                    Command="{Binding Command}"
                    FontWeight="Bold" FontSize="{Binding FontSize}"/>
                </DataTemplate>

你的指挥部应该是这样的:

代码语言:javascript
复制
        public ICommand Command => new DelegateCommand<object>(ExecuteCommand);

        private void ExecuteCommand(object obj)
        {
            double width = (double)obj;
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36826327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档