WPF:将TextBlock顶部对齐为不同的字体大小

时间:2018-12-12 16:42:25

标签: c# wpf textblock

我需要在顶部将两个字体大小不同的文本元素对齐。 到目前为止,我所要做的就是: enter image description here

此代码产生的结果:

                <Canvas VerticalAlignment="Center" HorizontalAlignment="Center" SnapsToDevicePixels="True">
                <StackPanel x:Name="RemainingTimeDisplay" Orientation="Horizontal" VerticalAlignment="Top">
                    <StackPanel.Margin>
                        <MultiBinding Converter="{StaticResource CenterConverter}">
                            <Binding ElementName="RemainingTimeDisplay" Path="ActualWidth"/>
                            <Binding ElementName="RemainingTimeDisplay" Path="ActualHeight"/>
                        </MultiBinding>
                    </StackPanel.Margin>
                    <!--<Run FontSize="360" Text="{Binding RemainingTime.Minutes, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, StringFormat=D2, Converter={StaticResource IntAbsConverter}}"/>
                        <Run FontSize="128" Text="{Binding RemainingTime.Seconds, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, StringFormat=D2, Converter={StaticResource IntAbsConverter}}"/>-->
                    <TextBlock FontSize="360" Text="{Binding RemainingTime.Minutes, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, StringFormat=D2, Converter={StaticResource IntAbsConverter}}"/>
                    <TextBlock FontSize="128" Text="{Binding RemainingTime.Seconds, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, StringFormat=D2, Converter={StaticResource IntAbsConverter}}"/>
                </StackPanel>
            </Canvas>

或: enter image description here

<Canvas VerticalAlignment="Center" HorizontalAlignment="Center" SnapsToDevicePixels="True">
  <StackPanel x:Name="RemainingTimeDisplay" Orientation="Horizontal" VerticalAlignment="Top">
    <StackPanel.Margin>
        <MultiBinding Converter="{StaticResource CenterConverter}">
            <Binding ElementName="RemainingTimeDisplay" Path="ActualWidth"/>
            <Binding ElementName="RemainingTimeDisplay" Path="ActualHeight"/>
        </MultiBinding>
    </StackPanel.Margin>
    <TextBlock FontFamily="Roboto" VerticalAlignment="Top">
      <TextBlock FontSize="360" Text="{Binding RemainingTime.Minutes, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, StringFormat=D2, Converter={StaticResource IntAbsConverter}}"/>
      <TextBlock FontSize="128" Text="{Binding RemainingTime.Seconds, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, StringFormat=D2, Converter={StaticResource IntAbsConverter}}"/>
     </TextBlock>
  </StackPanel>
</Canvas>

我还尝试了其他一些事情,在这里我不再在代码中重复。我尝试使用“ run”作为两个文本元素在单个TextBlock上的子级,并且我(实际上已经开始)尝试了不使用TextBlock封装其他两个元素的情况。

所以我尝试了很多,我用谷歌搜索了很多-但我仍然不是我想要的地方。

任何提示将不胜感激!

谢谢, 马库斯

1 个答案:

答案 0 :(得分:0)

尝试以下操作:

<TextBlock FontSize="360"
            Text="00"
            LineHeight="360"
            LineStackingStrategy="BlockLineHeight" />
<TextBlock FontSize="128"
            LineHeight="360"
            LineStackingStrategy="BlockLineHeight">
    <Run Text="00" BaselineAlignment="Top" />
</TextBlock>