Setup the PathListBox
If you’ve been working your way through this series of tutorials you should be pretty
familiar with this process, but we’ll walk through the steps so we share the same
starting scene.
- Create a new “Silverlight Application + Website” and name it “PLBTextOnAPath”.
- Draw a Path, change the Fill to “No Brush” and the Stroke to Blue.
- Change the StrokeThickness property to 3.
- In the Appearance pane, click the “Show advanced options” dropdown and change the
StrokeDashArray property to “1 2”.
- Change the StrokeDashCap property to “Triangle”.
- Right-click the Path and select the Path -> Make Layout Path option.
The Path is in place and we’ve dressed it up a little using advanced Stroke properties.
Plus the PathListBox has been created with a LayoutPath defined.
Add and Bind the Text Source
To make this example dynamic, we will use a TextBox control as the text source and
element binding to bind the Text property to the PathListBox ItemsSource property.
This way when a new text value is entered in the TextBox the PathListBox will be
automatically updated.
- Add a TextBox control above the Path
- With the PathListBox selected, click the Advanced options square (
) next to the ItemsSource property and select
the “Element Property Binding…” option.
- Move your mouse over the TextBox and you will notice the icon has changed to the
Element Picker icon (
)
- Click the TextBox and in the Create Data Binding dialog, select Text and hit OK.
After the Text property is bound to the ItemSource property, the text value becomes
a collection of letters and each letter is rendered within a PathListBoxItem along
the Path. And that’s all there is to it, although the text is a little hard to see.
Enhance the Text Display
The default PathListBoxItems template contains a ContentPresenter control which
by default displays string values using a TextBlock control. Rather than let the
ContentPresenter decide how the letters appear, we will define a custom ItemTemplate.
- In the Objects panel, drag the Path, below the PathListBox.
- Right-click the PathListBox and select the Edit Additional Templates -> Edit Generated
Items (ItemTemplate) -> Create Empty… option.
- On the Create DataTemplate Resource dialog hit OK.
- Now in template editing mode add a TextBlock to the Grid, by double clicking the
TextBox tool icon in the Tools panel.
- Click the Advanced options square (
) next to the Text property and select the Data Binding… option.
- In the Create Data Binding dialog, click the Data Context tab, select “String” and
hit OK.
- Set the FontFamily property to “Arial Black” and FontSize to 36 pt.
- Change the Foreground to a green color.
- Set the Translate Y property to -26, so the letter will be placed above the line.
The custom template does a much better job showing the text, now we will align the
letters to the path to create an improved display.
- Exit template editing mode.
- Select the PathListBox and change the Orientation property to “OrientToPath”.
- Lower the Padding to -37, effectively reducing the letter-spacing.
- Run the project and type a new text value into the TextBox.