Click here to Skip to main content
15,894,740 members
Articles / Desktop Programming / WPF

Text On A Path in WPF

Rate me:
Please Sign up or sign in to vote.
4.81/5 (30 votes)
30 Oct 2008CPOL2 min read 124.7K   4.3K   78  
A control that will display a text string on a specified geometry
<Window x:Class="TextOnAPathTestApp.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:TextOnAPath="clr-namespace:TextOnAPath;assembly=TextOnAPath"
    Title="Text On a Path Test App" Height="500" Width="500">
    <Window.Resources>
        <!-- the geometries below are added to the combobox in code behind -->
        <PathGeometry x:Key="PathGeometry" Figures="M50,242 C120,361 230.5,276.5 230.5,276.5 L305.98807,182.3422 C305.98807,182.3422 419.5,179.5002 367.5,265.49993 315.5,351.49966 238.50028,399.49924 238.50028,399.49924 L61.500017,420.49911"/>
        <EllipseGeometry x:Key="EllipseGeometry" Center="50,100" RadiusX="50" RadiusY="100"/>
        <RectangleGeometry x:Key="RectangleGeometry" Rect="0,0,100,200"/>
        <LineGeometry x:Key="LineGeometry" StartPoint="50,0" EndPoint="100,100"/>        
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" MinHeight="79" />
            <RowDefinition Height="Auto" MinHeight="54" />
            <RowDefinition Height="Auto" MinHeight="30" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Border Grid.Row="0" BorderBrush="Black" BorderThickness="2">
            <StackPanel Orientation="Vertical" >
                <Label HorizontalAlignment="Left" Margin="5">Enter Text:</Label>
                <TextBox Margin="5" Name="textBox" Width="Auto"
                         HorizontalAlignment="Left" MinWidth="200">This is a really long test sentence that just seems to go on and on forever without any rhyme or reason but just to take up space.</TextBox>
            </StackPanel>
        </Border>
        
        <Border Grid.Row="1" BorderBrush="Black" BorderThickness="2">
            <StackPanel Orientation="Horizontal" >
                <Label HorizontalAlignment="Left" Margin="5" Name="label2" 
                       VerticalContentAlignment="Center">Select Geometry:</Label>
                <ComboBox Margin="5" Name="ComboBoxSelectedGeo" VerticalContentAlignment="Center"/>
            </StackPanel>
        </Border>
        
        <Border Grid.Row="2" BorderBrush="Black" BorderThickness="2">
            <StackPanel Orientation="Horizontal">
                <CheckBox HorizontalAlignment="Left" Margin="5" Name="drawGeometry">Draw Geometry?</CheckBox>
                <CheckBox Margin="15,5,5,5" Name="scaleGeometry">Scale Geometry?</CheckBox>
            </StackPanel>
        </Border>
    
        <TextOnAPath:TextOnAPath Grid.Row="3" Text="{Binding ElementName=textBox, Path=Text}"
                                 DrawPath="{Binding ElementName=drawGeometry, Path=IsChecked}"
                                 ScaleTextPath="{Binding ElementName=scaleGeometry, Path=IsChecked}"
                                 BorderBrush="Black" BorderThickness="2"
                                 TextPath="{Binding SelectedGeometry}"/>
    </Grid>
</Window>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I work in the Bay Area primarily developing software on the Windows platform using C++, .NET/C#, WPF, and Silverlight.

Comments and Discussions