WPF - copy/paste selection with custom font family
I 开发者_开发百科have a custom font family embedded in my WPF application which I can reference by specifying a base URI and font family name.
new FontFamily(new Uri("pack://application:,,,/Fonts/"), "./#My Custom Font Family");
However, when I copy a selection which uses this font the xaml on the clipboard resembles the following
<Run Text="Foo" FontFamily="./#My Custom Font Family" />
When I paste into the same RichTextBox I lose the font as it falls back to the system default because -
When a FontFamily is specified as an attribute in markup, the base URI value is always implied—its value is the URI of the XAML page. http://msdn.microsoft.com/en-us/library/system.windows.media.fontfamily.aspx
and my xaml page is not located in the same directory as the custom font family.
Any ideas for a workaround?
I ended up working around this by changing the way the font family was initialized.
new FontFamily(new Uri("pack://application:,,,/Fonts/"), "./#My Custom Font Family");
became
new FontFamily(new Uri("pack://application:,,,"), "MyAssemblyName;Component/Fonts/#My Custom Font Family");
when serialized to the clipboard (XAML)
<Run Text="Foo" FontFamily="Component/Fonts/#My Custom Font Family" />
and the end result - my custom font is preserved when copy/pasted within the rich text box.
精彩评论