简体   繁体   中英

How to vertically center a wingding character in textblock xaml / C# / UWP

I'm trying to use a Wingdings symbol as a graphic in a control and am trying to make it vertically centered within a textbox (UWP app.) I'm putting a visible boarder around it and the fact that the symbol is not vertically centered is noticeable and ruining what I'm going for. If it's possible it saves me from having to do custom art and the since the Wingdings are resizable my graphic could be somewhat easily rescaled. That's why I don't want to manually adjust the height until it looks centered.

Most the Wingdings aren't vertically centered but are sometimes used as graphics so I'm wondering if this is doable w/ reasonable effort. A slick xaml way to do this would be ideal but a font metric measuring and then textblock height adjusting tack would be good.

Here's an example of the problem. The hard drive symbol is not vertically centered in the border.

    <Border BorderBrush="Black" BorderThickness="10"  Background="White" HorizontalAlignment="Center" VerticalAlignment="Center">
        <TextBlock Text="&#x3b;" FontFamily="Wingdings" FontSize="230"/>
    </Border>

EDIT - changed example to declutter and to use Wingdings hard drive symbol because the skull&bones is close enough to vertically centered to confuse the question. The drive symbol is very clearly not vertically centered. My goal to automatically vertically center it even as the font size changes. I can see how XAML may not be able to do this but I'm wondering if some font/text metrics querying exists that I could use to do it.

EDIT 2 - Adding a picture (from above XAML which has the same issues as all the suggested XAML(s):

I'm trying to get the center of the symbol to be at the center of the textblock 我正在尝试使符号的中心位于文本块的中心

The skull & bones is not vertically centered in the border.

Actually by default the skull&bones text should be vertically center. The reason for why your code make it looks not vertically is that the font size is too larger to be inside the border, since Border's height is restricted to 200 . So the exceed parts of TextBlock you will not seen which may looks like it is not vertically center.

How to vertically center a wingding character in textblock xaml / C# / UWP

There are several ways to do this, choose which way depend on your prefer.

  1. Remove the height and width properties for the Border control you will see the text vertically center.

     <Border BorderBrush="Black" BorderThickness="10" Background="White" HorizontalAlignment="Center"> <TextBlock x:Name="textBlock" Text="N" FontFamily="Wingdings" FontSize="230" /> </Border> 
  2. Add a ViewBox control outside the TextBlock control. ViewBox can stretch and scale a single child to fill the available space. This will let your text looks always be center no matter how large is the font size. The text size will depend on the size of Border control. Code as follows:

     <Border BorderBrush="Black" BorderThickness="10" Height="200" Width="175" Background="White" > <Viewbox> <TextBlock x:Name="textBlock" Text="N" FontFamily="Wingdings" FontSize="230" /> </Viewbox> </Border> 
  3. Just use a TextBox control instead of the component of Border and TextBlock . This will reach the same effects that make the text always center and no matter what is the font size. Pay attention that don't set fixed height and width for the TextBox and also make the TextBox read only. Code as follows:

     <TextBox x:Name="textBox" HorizontalAlignment="Center" VerticalAlignment="Center" BorderBrush="Black" BorderThickness="10" FontFamily="Wingdings" FontSize="230" IsReadOnly="True" Text="N" TextAlignment="Center" /> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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