如何将字体大小绑定到可变网格大小 [英] How to bind Font Size to variable Grid Size

查看:133
本文介绍了如何将字体大小绑定到可变网格大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个2列和2行的网格。单个字符(Unicode U + 2699)被放置在右下方的网格区域内。它看起来像这样:



p>

我喜欢字符自动调整字体大小以适应已放置的网格字段(在这种情况下,它应该绑定到第二个网格的高度行,但由于在某些情况下可能不清楚网格的高度或宽度是否较小,因此知道如何绑定到这两个值的最低值也是很好的做法)。

到目前为止,我的实现如下所示(本例简化了一下):

 <网格和GT; 
< Grid.RowDefinitions>
< RowDefinition Height =*/>
< RowDefinition x:Name =heightToBindHeight =40/>
< /Grid.RowDefinitions>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =14 */>
< ColumnDefinition Width =*/>
< /Grid.ColumnDefinitions>

< Button FontSize ={Binding ElementName = heightToBind,Path = Height.Value,Mode = OneWay}Content =&#x2699; Grid.Row =1Grid.Column =1Horizo​​ntalAlignment =Right/>
< / Grid>

这里的问题是,它仅在高度为固定值<在RowDefinitions里面。我希望它能与以下定义一起工作:

 < Grid.RowDefinitions> 
< RowDefinition Height =4 */>
< RowDefinition x:Name =heightToBindHeight =*/>
< /Grid.RowDefinitions>

作为一个额外的问题,我也很感兴趣,为什么这个角色可能太低所以它在底部被切断(我尝试了 VerticalAlignment =Center作为按钮,但没有任何效果)。

ViewBox 作为按钮的内容:

 < Button Grid.Row =1Grid.Column =1> 
< Button.Content>
< Viewbox StretchDirection =BothHorizo​​ntalAlignment =Stretch>
< / Viewbox>
< /Button.Content>
< / Button>

A ViewBox 可以扩展和缩放他的孩子以填充所有可用空间......


I have a grid with 2 columns and 2 rows. A single character (Unicode U+2699) was placed inside the bottom right grid field. It looks like this:

I´d like the character to automatically adjust its font size to fit the grid field it has been placed in (in this case it should be bound to the height of the second grid row, but since in some cases it could be unclear if the height or the width of the grid is smaller, it would be also nice to know how to bind to the lowest of those 2 values).

My implementation so far is something like the following (I simplified it a bit for this example):

<Grid>
   <Grid.RowDefinitions>
      <RowDefinition Height="*"/>
      <RowDefinition x:Name="heightToBind" Height="40"/>
   </Grid.RowDefinitions>
   <Grid.ColumnDefinitions>
      <ColumnDefinition Width="14*" />
      <ColumnDefinition Width="*" />
   </Grid.ColumnDefinitions>

   <Button FontSize="{Binding ElementName=heightToBind, Path=Height.Value, Mode=OneWay}" Content="&#x2699;" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Right" />
</Grid>

The problem here is, that it only works if the height is a fixed value inside the RowDefinitions. I want it to work with the following definition:

<Grid.RowDefinitions>
   <RowDefinition Height="4*"/>
   <RowDefinition x:Name="heightToBind" Height="*"/>
</Grid.RowDefinitions>

As a bonus question I´d also be interested why it might be that the character is placed too low so it is getting cut off at the bottom (I tried VerticalAlignment="Center" for the button but with no effect).

解决方案

You can try using a ViewBox as the button's content:

<Button Grid.Row="1" Grid.Column="1">
    <Button.Content>
        <Viewbox  StretchDirection="Both" HorizontalAlignment="Stretch">
            <TextBlock Text="&#x2699;" />
        </Viewbox>
    </Button.Content>
</Button>

A ViewBox can stretch and scale his child to fill all the available space...

这篇关于如何将字体大小绑定到可变网格大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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