Gaurang
Gaurang

Reputation: 371

Calculate perfect width of TextBlock with text containing unicode characters

I have the following text in a TextBlock:

textBlock1.Text="Welcome! The \u25Bc Unicode Consortium enables people \u25Bc around the world to use computers \u25Bc in any language. Our freely-available specifications \u25Bc and data form \u25Bc the foundation for software \u25Bc internationalization in all \u25Bc major operating systems, search engines, applications, and the World Wide Web."

properties of TextBlock :

textBlock1.FontSize=22;
textBlock1.FontFamily="Segoe UI";

Here \u25Bc is my special unicode character. How can I calculate the perfect width of the TextBlock without any variations? The width needs to be perfect as I am using it to provide animation to textBlock1.

Upvotes: 0

Views: 182

Answers (1)

GazTheDestroyer
GazTheDestroyer

Reputation: 21261

ActualWidth works for me:

<Window x:Class="widthtest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">

<StackPanel HorizontalAlignment="Left">

    <TextBox x:Name="text" 
             FontFamily="Segoe UI" 
             FontSize="22" 
             Text="Welcome! &#x25BC; The &#x25BC; Unicode Consortium" />

    <Border Margin="0,10" 
            Background="Blue" 
            Height="{Binding ActualHeight, ElementName=text}" 
            Width="{Binding ActualWidth, ElementName=text}" />

</StackPanel>
</Window>

enter image description here

Upvotes: 1

Related Questions