将自定义 FontFamily 绑定到 TextBlock 列表

Binding Custom FontFamilys to a list of TextBlocks

我很确定我的做法是错误的,但如果能帮助我朝着正确的方向前进,我们将不胜感激。

基本上,我尝试添加一个自定义字体列表,然后将它们显示在一个 TextBlock 列表中,但无论我尝试多少种不同的方法,我都会不断收到同样的错误,但这并没有对我来说很有意义。

Error: BindingExpression path error: 'FontFamilyList' property not found on 'Windows.UI.Xaml.Media.FontFamily'. BindingExpression: Path='FontFamilyList' DataItem='Windows.UI.Xaml.Media.FontFamily'; target element is 'Windows.UI.Xaml.Controls.TextBlock' (Name='FontSelector'); target property is 'FontFamily' (type 'FontFamily')

最终结果应该是一行 24 个 TextBlock,每个都包含字符串 "abcdef",但每个都使用不同的字体。

这是我目前为止尝试过的方法。

Class

public static class CustomFonts
{
    public static FontFamily sketchy = new FontFamily("/Assets/font/a_bit_sketchy.ttf#sketchy");
    public static FontFamily adam = new FontFamily("/Assets/font/adam.cg_pro.otf#adam");
    public static FontFamily addled = new FontFamily("/Assets/font/addled.TTF#addled");
    public static FontFamily adventure = new FontFamily("/Assets/font/adventure.ttf#adventure");
    public static FontFamily agentorange = new FontFamily("/Assets/font/agentorange#agentorange");
    public static FontFamily arial = new FontFamily("/Assets/font/arial.ttf#arial");
    public static FontFamily bira = new FontFamily("/Assets/font/bira.ttf#bira");
    public static FontFamily blueprint = new FontFamily("/Assets/font/blueprint.otf#blueprint");
    public static FontFamily branbollFet = new FontFamily("/Assets/font/branbollFet.ttf#branbollFet");
    public static FontFamily budmo = new FontFamily("/Assets/font/budmo_jiggler.otf#budmo");
    public static FontFamily childs = new FontFamily("/Assets/font/childs.ttf#childs");
    public static FontFamily collegiate = new FontFamily("/Assets/font/collegiate.ttf#collegiate");
    public static FontFamily comesinhandy = new FontFamily("/Assets/font/comesinhandy.ttf#comesinhandy");
    public static FontFamily dknanuk = new FontFamily("/Assets/font/dk_nanuk.otf#dknanuk");
    public static FontFamily itcedscr = new FontFamily("/Assets/font/itcedscr.TTF#itcedscr");
    public static FontFamily levi = new FontFamily("/Assets/font/levi_brush.TTF#levi");
    public static FontFamily lobster = new FontFamily("/Assets/font/lobster.otf#lobster");
    public static FontFamily missionscript = new FontFamily("/Assets/font/mission_script.otf#mission");
    public static FontFamily moonbold = new FontFamily("/Assets/font/moon_bold.otfmoonbold");
    public static FontFamily moonlight = new FontFamily("/Assets/font/moon_light.otf#moonlight");
    public static FontFamily permanentmarker = new FontFamily("/Assets/font/permanentmarker.ttf#permanent");
    public static FontFamily phosphate = new FontFamily("/Assets/font/phosphate.ttc#phosphate");
    public static FontFamily ralewaydots = new FontFamily("/Assets/font/raleway_dots.ttf#raleway");
    public static FontFamily rosewood = new FontFamily("/Assets/font/rosewood.otf#rosewood");

    public static List<FontFamily> FontFamilyList;

    public static List<FontFamily> PopulateFonts()
    {
        FontFamilyList = new List<FontFamily>
        {
            sketchy,
            adam,
            addled,
            adventure,
            agentorange,
            arial,
            bira,
            blueprint,
            branbollFet,
            budmo,
            childs,
            collegiate,
            comesinhandy,
            dknanuk,
            itcedscr,
            levi,
            lobster,
            missionscript,
            moonbold,
            moonlight,
            permanentmarker,
            phosphate,
            ralewaydots,
            rosewood
        };
        return FontFamilyList;
    }
}

设置项目来源

public DesignView()
{
    InitializeComponent();
    FontList.ItemsSource = CustomFonts.PopulateFonts();
}

XAML

<Grid Height="150" Margin="0,270,0,0">
    <Image Source="ms-appx:///Resources/Elements/prev_250.png" Height="100" Width="100" HorizontalAlignment="Left"></Image>
    <ListView Name="FontList" ItemsSource="{Binding CustomFonts}"
                ScrollViewer.HorizontalScrollMode="Enabled"
                ScrollViewer.HorizontalScrollBarVisibility="Auto"
                ScrollViewer.IsHorizontalRailEnabled="True" VerticalAlignment="Center" HorizontalAlignment="Center"
                Margin="100,270,100,20"
                Height="70" AutomationProperties.SizeOfSet="24">
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid VerticalAlignment="Bottom" Margin="0,0,0,0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                        <TextBlock Name="FontSelector" Text="abcdef" Grid.Column="0" Height="70" Width="70" Margin="1" VerticalAlignment="Center" FontFamily="{Binding FontFamilyList}">
                    </TextBlock>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    <Image Source="ms-appx:///Resources/Elements/next_250.png" Height="100" Width="100" HorizontalAlignment="Right"></Image>
</Grid>

据我所知,你搞砸了 Binding/DataContext

在 XAML 中,您将 ItemsSource 指定为自定义字体 class

<ListView Name="FontList" ItemsSource="{Binding CustomFonts}"

DesingView 代码破坏了此绑定并将列表设置为 ItemsSource - 对我来说这似乎是正确的 ItemsSource

FontList.ItemsSource = CustomFonts.PopulateFonts();

因此,您的 DataTemplate 中有一个不同的 DataContext。 正如 XAML 所指定的那样,您的列表将包含一个带有 DataContext CustomFont 的项目。 您的 DesignView 将有 24 个带有 DataContext FontFamily 的项目。

要使您的 DesignView 正常工作,您必须将 DataTemplate 中的 FontFamily 绑定更新为

<TextBlock Name="FontSelector" Text="abcdef" Grid.Column="0" Height="70" Width="70" Margin="1" VerticalAlignment="Center" FontFamily="{Binding}">

我做了一个简单的项目来了解为什么会出现问题以及它是如何工作的。正如@eberthold 提到的,您已经覆盖了从 UI 开始工作的 ItemsSource。取而代之的是,您可以只绑定 DataContext。此外,您不必使用附加方法来创建列表。您可以在 class.

的构造函数中执行此操作

我将你的 class 从 Static 更改为 Non-Static。为了测试,我将系统字体添加到列表中。

public class CustomFonts
{
    public FontFamily sketchy = new FontFamily("/Assets/font/a_bit_sketchy.ttf#sketchy");
    public FontFamily adam = new FontFamily("/Assets/font/adam.cg_pro.otf#adam");
    public FontFamily addled = new FontFamily("/Assets/font/addled.TTF#addled");
    public FontFamily adventure = new FontFamily("/Assets/font/adventure.ttf#adventure");
    ...

    public List<FontFamily> FontFamilyList { get; set; }

    public CustomFonts()
    {
        FontFamilyList = new List<FontFamily>
        {
            new FontFamily("Times New Roman"),
            new FontFamily("Monotype Corsiva"),
            new FontFamily("Cambria"),
            new FontFamily("Calibri")
        };

        //FontFamilyList = new List<FontFamily>
        //{
        //    sketchy,
        //    adam,
        //    addled,
        //    adventure,
        ///   ....
        //};
    }
}

这是我的XAML

<Grid>
    <ListView Name="FontList" AutomationProperties.SizeOfSet="24" ItemsSource="{Binding FontFamilyList}">
        <ListView.DataContext>
            <app4:CustomFonts />
        </ListView.DataContext>
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="ABCDEF" FontFamily="{Binding }"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

所以我直接将XAML中FontListDataContext设置为CustomFontsclass。然后我将 FontFamiltList 属性 直接绑定到 ItemsSoure。由于您想将 FontFamily 绑定到 TextBoxFontFamily 属性,并且这是列表中的元素,因此它只是 {Binding }

下面是输出。