English 中文(简体)
How to display all installed fonts in a ComboBox
原标题:

I need to display all the user s installed fonts in a WinUI 3 ComboBox. I m using this code:

<ComboBox x:Name="FontComboBox" ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Source}" FontFamily="{Binding Source}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

But this causes the error:

The type x:Static was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.

Not sure what an assembly reference is.

What would I do to fix this?

问题回答

AFAIK, WinUI 3 doesn t support x:Static.

So, let me show you another way to achieve this using the Win2D NuGet package.

<Page
    x:Class="FontFamiliesExample.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="using:FontFamiliesExample"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:win2d="using:Microsoft.Graphics.Canvas.Text"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
    mc:Ignorable="d">

    <StackPanel>
        <ComboBox ItemsSource="{x:Bind win2d:CanvasTextFormat.GetSystemFontFamilies()}" />
    </StackPanel>

</Page>

WinUI 3 doesn t support x:Static . And WinUI 3 also doesn t support Fonts.SystemFontFamilies`.

If you want to display all installed fonts. I suggest you could try to install the Win2D Nuget package.

enter image description here

And then you could use CanvasTextFormat.GetSystemFontFamilies Method to get a list of font families available. Here is the code:

  public List<string> Fonts
    {
        get
        {
            return CanvasTextFormat.GetSystemFontFamilies().OrderBy(f => f).ToList();
        }
    }




<ComboBox x:Name="FontsCombo"
        ItemsSource="{x:Bind Fonts}">

    </ComboBox>




相关问题
WPF Datagrid, Setting the background of combox popup

I would like to change the color of the popup background when using a DatagridComboboxColumn in the WPF Toolkit datagrid. I ve edited the Template for a normal Combobox and it works great for selected ...

How to insert ComboBox item into ListBox? [winforms]

The question is very simple, How to insert ComboBox selected item into ListBox using c#? I have tried with this: listbox.Items.Add(combobox.SelectedItem); and some other permutations but it always ...

How do I bind a ComboBox to a one column list

I ve seen how to bind a ComboBox to a list that has columns like this: ItemsSource="{Binding Path=Entries}" DisplayMemberPath="Name" SelectedValuePath="Name" SelectedValue="{Binding Path=Entry}" But ...

Wpf Combobox Limit to List

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first ...

热门标签