简体   繁体   English

如何根据其内容垂直对齐文本框

[英]How to vertically align a TextBox based on its content

I have a text block I want to vertically align so it appears in the center of a circle. 我有一个要垂直对齐的文本块,因此它出现在圆圈的中心。 It's a single character, such as 'a', 'y', '?', 'R' or any other valid character. 它是单个字符,例如“ a”,“ y”,“?”,“ R”或任何其他有效字符。 It could have a descender, ascender, be a capital, non alphabetical etc. 它可以有一个降序,升序,大写字母,非字母等。

With the following code, it appears to align the text block according to the height of a capital letter. 使用以下代码,似乎可以根据大写字母的高度对齐文本块。 So a lower case letter appears too low, and letter with a descender even more so. 因此,小写字母显得太小,而带有降序的字母显得更小。

在此处输入图片说明 在此处输入图片说明

    <Grid Width="32" Background="Green"  Height="32">
        <Ellipse Width="32" Height="32" Stroke="Red" Fill="White"/>
        <TextBlock Text="{Binding Character}" Foreground="Blue" HorizontalAlignment="Center" FontWeight="Bold" VerticalAlignment="Center" FontFamily="Arial"  FontSize="28"/>
    </Grid>

Is there a way to get to align vertically based on the text content of the TextBlock, instead of the reserved area of the text that might be in it? 有没有一种方法可以基于TextBlock的文本内容而不是其中可能保留的文本区域垂直对齐?

If it is not possible, what other controls / methods are available in WPF to achieve this? 如果不可能,WPF中还有哪些其他控件/方法可以实现此目的?

A more complete sample that works for me - 对我有用的更完整的示例-

<ControlTemplate TargetType="{x:Type local:LevelControl}">
                <Viewbox x:Name="Frame">
                    <Grid Margin="2" Background="Transparent" VerticalAlignment="Center" HorizontalAlignment="Center" Width="26" Height="26">
                        <Ellipse x:Name="outerEllipse" Fill="{Binding Fill, ElementName=innerEllipse}" Stretch="UniformToFill" Grid.Row="0" Opacity="0.5" />
                        <Ellipse x:Name="innerEllipse" Fill="Gray" Stretch="UniformToFill" Margin="4" Grid.Row="0" />
                        <TextBlock x:Name="Label" FontWeight="Bold" FontSize="18" Foreground="White" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Margin="7.948,0,7.698,2.125" />
                    </Grid>
                </Viewbox>

            </ControlTemplate>

or try a label <Label VerticalAlignment="Center" VerticalContentAlignment="Center" ></Label> 或尝试使用标签<Label VerticalAlignment="Center" VerticalContentAlignment="Center" ></Label>

Landerah in you code only if i set magin for textblock, than i can set text where i want. 只有在我为textblock设置margin的情况下,您中的Landerah才可以编码,否则我可以在需要的地方设置文本。 You can also set margin dynamically ie If character is lower case set magin bit above like test.Margin = new Thickness(0, -2, 0, 0); 您也可以动态设置边距,即如果字符是小写字母,则在test上方设置边距位。Margin = new Thickness(0,-2,0,0);

 <Grid Width="32" Background="Green">
        <Ellipse Width="32" Height="32" Stroke="Red" Fill="White"/>
        <TextBlock Margin="0,-2,0,0" Text="r" Foreground="Blue" HorizontalAlignment="Center" FontWeight="Bold" VerticalAlignment="Center" FontFamily="Arial"  FontSize="28"/>
 </Grid> 

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

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