Search code examples
wpfdata-binding

WPF Data Binding - Displaying the list of system fonts


Using XAML, I am trying to get a list box to display the list of system fonts.

I am not sure exactly what to type in the Bindings string.

Here's my attempt:

<Window x:Class="ListDataBinding.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:m="clr-namespace:System.Windows.Media;assembly=PresentationCore" 
    Title="Window1" Height="300" Width="300">
    <Grid>
        <ListBox ItemsSource="{Binding Source={StaticResource m:Fonts.SystemFontFamilies}}"></ListBox>

    </Grid>
</Window>

I get an error: Cannot find resource named '{m:Fonts.SystemFontFamilies}'

I'm wondering what I should be typing. I haven't managed to find a helpful MSDN page on this - if somebody could point me to a link I'd be grateful.


Solution

  • <Window x:Class="ListDataBinding.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:m="clr-namespace:System.Windows.Media;assembly=PresentationCore" 
        Title="Window1" Height="300" Width="300">
       <Window.Resources>
            <ObjectDataProvider x:Key="fontFamiliesKey" ObjectType="{x:Type m:Fonts}" MethodName="get_SystemFontFamilies"/>
        </Window.Resources>
        <Grid>
            <ListBox ItemsSource="{Binding Source={StaticResource fontFamiliesKey}}"></ListBox>
    
        </Grid>
    </Window>
    

    Try this