Create a Fonts-ComboBox using WPF
Some of you might know the comboboxes showing the available fonts including a preview of the font itself. This can be done very easily in WPF.
For this you need the following definition of a ComboBox in XAML:
<ComboBox x:Name=”FontChooser” ItemsSource=”{Binding Source={StaticResource myFonts}}” SelectedIndex=”0″> <ComboBox.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel /> </ItemsPanelTemplate> </ComboBox.ItemsPanel> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text=”{Binding}” FontFamily=”{Binding}”/> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>
Further more you need a ressource in your, for example, window:
<CollectionViewSource Source=”{Binding Source={x:Static Fonts.SystemFontFamilies}}” x:Key=”myFonts”/>
That’s all. There is no extra source code. Nothing. The result looks like as follows:
May 1st, 2008 at 4:18 am
good, it’s useful. thanks.
July 18th, 2008 at 8:13 am
[…] has the list of the fonts on the machine. I searched around and found part of what I needed at Norbert Eder’s WPF blog. (BTW, congrats on being a newly minted […]
July 18th, 2008 at 8:29 am
[…] has the list of the fonts on the machine. I searched around and found part of what I needed at Norbert Eder's WPF blog. (BTW, congrats on being a newly minted […]