简体   繁体   English

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

[英]WPF: Align TextBlock with different font sizes at top

I need to align two text elements of different font sizes at the top line. 我需要在顶部对齐两个不同字体大小的文本元素。 All I managed so far is either this : 到目前为止,我所要做的就是: 在此处输入图片说明

Resulting from this code: 来自此代码的结果:

                <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>

Or: 要么: 在此处输入图片说明

<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>

I tried also some more things, that I don't repeat here in code. 我还尝试了其他一些事情,在代码中不再赘述。 I tried using "run" as a child of on single TextBlock for both text elements and I (actually started) tried without the TextBlock that encloses the other two. 我尝试使用“ run”作为两个文本元素在单个TextBlock上的子级,而我(实际上已开始)尝试了不使用TextBlock封装其他两个元素的情况。

So I tried a lot, I googled a lot - and I'm still not where I want to be. 因此,我尝试了很多,我用Google搜索了很多-但仍然不是我想去的地方。

Any hint would be really appreciated! 任何提示将不胜感激!

Thanks, Marcus 谢谢,马库斯

Try the following: 请尝试以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM