Hey, I am trying to create a BarCode input box. On it a textblock is showing a preview of what the text input will look like in the specified Barcode type. I have a bunch of .TTF files that are the barcode fonts, they have been used in a WinForms app so I am sure they are correct. I try to load them to memory (don't want to install them) using:
sBarCodeFonts = new PrivateFontCollection();
unsafe
{
fixed (byte* p = Resources.Code39)
{
IntPtr MyIntPtr = (IntPtr)p;
sBarCodeFonts.AddMemoryFont(MyIntPtr, Resources.Code39.Length);
}
fixed (byte* p = Resources.Code128b)
{
IntPtr MyIntPtr = (IntPtr)p;
sBarCodeFonts.AddMemoryFont(MyIntPtr, Resources.Code128b.Length);
}
fixed (byte* p = Resources.i2of5)
{
IntPtr MyIntPtr = (IntPtr)p;
sBarCodeFonts.AddMemoryFont(MyIntPtr, Resources.i2of5.Length);
}
fixed (byte* p = Resources.ean13)
{
IntPtr MyIntPtr = (IntPtr)p;
sBarCodeFonts.AddMemoryFont(MyIntPtr, Resources.ean13.Length);
}
}
This seems to load the PrivateFontCollection correctly, quickwatch indicates so.
In the Barcode class, I have a MyFontFamily property, which contains the System.Media.FontFamily that corresponds to the loaded file. This property is loaded like this:
MyFontFamily = new System.Windows.Media.FontFamily(sBarCodeFonts.Families[0].Name);
And it seems to be loaded correctly as well.
Then, I have a Barcode object, and I'm using a TextBlock to display it's text, using it's FontFamily:
<TextBlock Text="{Binding Path=MyBarcode.TextContent, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}, Mode=OneWay}"
FontFamily="{Binding Path=MyBarcode.MyFontFamily, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}, Mode=OneWay}"
Name="txt"
Grid.Row="2" />
The TextBlock displays the text using a default font every time. I've debugged and the FontFamily is correctly set to one of the loaded ones in the previous C# code.
Any clues?
Thanks.
EDIT: Trying to simplify the problem, I've created a very quick and dirty test app to load the TTF and show it, this is the only code (besides the XAML with only a grid):
System.Windows.Media.FontFamily lFamily = new System.Windows.Media.FontFamily(new Uri(@"E:\Prototypes\TestApp\Resources\Code128b.ttf", UriKind.Absolute), "Code128bWin");
TextBlock lTextBlock = new TextBlock();
lTextBlock.Text = "jsldkasjLKJOSDIFUEWR32849792837.,´` ";
lTextBlock.FontFamily = lFamily;
lTextBlock.FontSize = 50.0;
grid.Children.Add(lTextBlock);
And it still shows the default font.