« A new type of town: Lily Valley | Main | Utah. »

Simple XAML Font Viewer

Admittedly, I forgot why I started writing this tiny program, so, before I put it to rest (or remember what my original goal was), I thought I'd post what little code I had for a tiny font viewer.

What's interesting about this code?

  1. The tooltip is styled. Actually, very plainly as a matter of fact, demonstrating how the pop-up can not only change dramatically, but also how it can be free of any bounds entirely (henc the see-thru look).
  2. I demonstrated how a DataTemplate can actually refer to itself (by binding the Tooltip to the current item's DataTemplate).
  3. It shows how you can bind to some simple sources without a line of code (only XAML) using CollectionViewSource.
  4. It also might help you to understand that when you bind to something like a listbox (or anything really), the Binding source doesn't go away -- it's just some visual's layered on top. So, here: <TextBlock FontFamily="{Binding}"> . Sure, this looks strange, but what it's doing is for each Font, it's using the FontFamily object and setting it to the FontFamily of the TextBlock. By using a "naked" binding reference, it's referring directly to the object being bound rather than specifying any named properties (using the Path syntax)
  5. It's also easy to write a fancy list box. Imagine how long this would take to do in Win32!

If you have XAMLPad installed (which if you have the .NET 3.0 SDK you do!), you can copy this XAML and try it.

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Height="508" Width="504"

>

<Page.Resources>

<CollectionViewSource Source="{x:Static Fonts.SystemFontFamilies}" x:Key="FontList" />

<Style TargetType="{x:Type ToolTip}">

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="{x:Type ToolTip}">

<Border >

<ContentPresenter />

</Border>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

<DataTemplate DataType="{x:Type FontFamily}">

<WrapPanel>

<StackPanel x:Name="MyVisual">

<TextBlock Text="{Binding Source}" x:Name="MyTextName" />

<TextBlock FontFamily="{Binding}">

abcdefghijklmnopqrstuvwxyz

</TextBlock>

<TextBlock FontFamily="{Binding}">

ABCDEFGHIJKLMNOPQRSTUVWXYZ

</TextBlock>

<TextBlock FontFamily="{Binding}">

1234567890

</TextBlock>

<TextBlock FontFamily="{Binding}">

!@#$%^&*()_+-=[]{}\|;:'",./><?

</TextBlock>

<StackPanel.ToolTip>

<Viewbox Width="500" Height="200">

<Label HorizontalAlignment="Left">

<Binding />

</Label>

</Viewbox>

</StackPanel.ToolTip>

</StackPanel>

</WrapPanel>

</DataTemplate>

</Page.Resources>

<Grid>

<Grid.RowDefinitions >

<RowDefinition Height="Auto" />

<RowDefinition />

</Grid.RowDefinitions>

<Label Content="Fonts" Grid.Row="0" />

<ListBox Grid.Row="1" Margin="6" ItemsSource="{Binding}" DataContext="{DynamicResource FontList}" />

</Grid>

</Page>

Help support my web site by searching and buying through Amazon.com (in assocation with Amazon.com).