2

I've added a few custom fonts to a Silverlight project. However, their behavior is very sporadic. I'm using it in a custom control. When I first add the control, it displays fine. When I rebuild, the font changes back to default. When I view the app in a browser, it also uses the default font. The font is embedded as a resource.

Silverlight issue

The top is right after I've added the control to the designer, the bottom is the app in the browser. I have no clue what is causing this. If you need the code for the control, I can provide it.

BorderedTextBlock.xaml

<UserControl x:Class="MindWorX.CustomPropertyReproduction.Controls.BorderedTextBlock"
    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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" Height="100" Width="200">

    <Grid x:Name="LayoutRoot">
        <Border BorderThickness="1" BorderBrush="Lime">
            <TextBlock Name="MainTextBlock" Margin="4" TextWrapping="Wrap" />
        </Border>
    </Grid>
</UserControl>

BorderedTextBlock.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace MindWorX.CustomPropertyReproduction.Controls
{
    public partial class BorderedTextBlock : UserControl
    {
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(BorderedTextBlock), new PropertyMetadata("TextBlock", new PropertyChangedCallback(OnTextChanged)));

        private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            BorderedTextBlock sender = (d as BorderedTextBlock);
            sender.MainTextBlock.Text = (String)e.NewValue;
        }

        new public static readonly DependencyProperty FontFamilyProperty = DependencyProperty.Register("FontFamily", typeof(FontFamily), typeof(BorderedTextBlock), new PropertyMetadata(new FontFamily("Portable User Interface"), new PropertyChangedCallback(OnFontFamilyChanged)));

        private static void OnFontFamilyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            BorderedTextBlock sender = (d as BorderedTextBlock);
            sender.MainTextBlock.FontFamily = (FontFamily)e.NewValue;
        }

        new public static readonly DependencyProperty FontSizeProperty = DependencyProperty.Register("FontSize", typeof(Double), typeof(BorderedTextBlock), new PropertyMetadata(11d, new PropertyChangedCallback(OnFontSizeChanged)));

        private static void OnFontSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            BorderedTextBlock sender = (d as BorderedTextBlock);
            sender.MainTextBlock.FontSize = (Double)e.NewValue;
        }

        public BorderedTextBlock()
        {
            InitializeComponent();
        }

        public String Text
        {
            get { return (String)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        new public FontFamily FontFamily
        {
            get { return (FontFamily)GetValue(FontFamilyProperty); }
            set { SetValue(FontFamilyProperty, value); }
        }

        new public Double FontSize
        {
            get { return (Double)GetValue(FontSizeProperty); }
            set { SetValue(FontSizeProperty, value); }
        }
    }
}
William
  • 772
  • 7
  • 18
  • If you can add the syntax and project structure you're using with the custom font that would help a lot with trying to figure out your issue. – Joe McBride Jan 18 '11 at 06:03
  • Heres a reproduction of the problem. If you test, you'll see it works fine with preexisting fonts. But not with custom fonts. http://dl.dropbox.com/u/992656/MindWorX.CustomPropertyReproduction.7z – William Jan 19 '11 at 23:40
  • can't figure out what a .7z extension is in order to pull it off of dropbox and see your sample. Could you post your code above and that will help us determine what could be the issue? – Todd Main Jan 20 '11 at 08:23
  • @Otaku: Added the source for the control to the question. You'll have to find a custom font to test it with yourself. – William Jan 20 '11 at 14:57

1 Answers1

2

Okay, I see what the problem is here, but I don't know how to fix this procedurally.

The issue is that specifying the FontFamily of the custom font doesn't matter (i.e. it won't work) if you do not qualify it with it's name/path like Fonts\mynewfont.ttf#My New Font. What the code above is doing is just saying "bring up My New Font" which at run time it doesn't understand because it can't be found.

One way to get around this is to make the fonts you need as resources in App.xaml and use them that way, like: <FontFamily x:Key="YanoneKaffeesatzThin">Fonts\Yanone Kaffeesatz-47.ttf#Yanone Kaffeesatz Thin</FontFamily>1 and then call application of this family from code: myTxtBox.FontFamily = DirectCast(App.Current.Resources("YanoneKaffeesatzThin"), FontFamily).

1Yanone Kaffeesatz Thin is from the Google Fonts library

Todd Main
  • 28,951
  • 11
  • 82
  • 146
  • +1 for the resource xaml example. However, the issue isn't when assigning from code, but when i use the designer to assign the font. I made another custom control, where I not only implemented custom font, but also the 4 other font related fields. After doing this, it seems it's working as it should. I have no idea why, but it looks like it fixed the issue. – William Jan 25 '11 at 17:01