Reputation: 161
I'm having troubles binding to a static FontFamily.
Here's some info about what i'm actually doing. In the application I want users to chose their font. So i allow them to choose the font at the start then it saves to a file (to make it save the font for next time the user opens the application.) Then i read from the file and store the font in a static variable under the class 'Fonts'. When i use MessageBoxes to display the fonts it works. So the only thing that isn't working is my binding to the font. Please have a look.
public class Fonts
{
private static FontFamily _titleFont;
/// <summary>
/// A static property which you'd like to bind to
/// </summary>
public static FontFamily titleFont
{
get
{
return _titleFont;
}
set
{
_titleFont = value;
// Raise a change event
OnTitleFontChanged(EventArgs.Empty);
}
}
// Declare a static event representing changes to your static property
public static event EventHandler TitleFontChanged;
// Raise the change event through this static method
protected static void OnTitleFontChanged(EventArgs e)
{
EventHandler handler = TitleFontChanged;
if (handler != null)
{
handler(null, e);
}
}
static Fonts()
{
// Set up an empty event handler
TitleFontChanged += (sender, e) => { return; };
}
}
Then on the start method of window it sets static variable like this:
Fonts.titleFont = new FontFamily(new Uri("pack://application:,,,/Fonts/#Aldrich", UriKind.Absolute), "Aldrich");
The Textblocks fontfammily is set like this:
<TextBlock x:Name="CurrentUserStatsTitle" FontSize="36" Text="Welcome, " Foreground="White" FontFamily="{Binding Path=(local:Fonts.titleFont)}"/>
Update:
This is how the code looks now. everything runs just fine but my binding is still not working. Is there anything i'm doing wrong ? if so can you explain or send me a link to a explained solution?
public class Fonts
{
private static FontFamily _titleFont;
/// <summary>
/// A static property which you'd like to bind to
/// </summary>
public static event PropertyChangedEventHandler StaticPropertyChanged;
public static FontFamily TitleFont
{
get { return _titleFont; }
set
{
_titleFont = value;
StaticPropertyChanged?.Invoke(null,
new PropertyChangedEventArgs(nameof(TitleFont)));
}
}
private static FontFamily _subTitleFont;
/// <summary>
/// A static property which you'd like to bind to
/// </summary>
public static FontFamily SubTitleFont
{
get { return _subTitleFont; }
set
{
_subTitleFont = value;
StaticPropertyChanged?.Invoke(null,
new PropertyChangedEventArgs(nameof(SubTitleFont)));
}
}
private static FontFamily _descriptionFont;
/// <summary>
/// A static property which you'd like to bind to
/// </summary>
public static FontFamily DescriptionFont
{
get { return _descriptionFont; }
set
{
_descriptionFont = value;
StaticPropertyChanged?.Invoke(null,
new PropertyChangedEventArgs(nameof(DescriptionFont)));
}
}
static Fonts()
{
StaticPropertyChanged += (sender, e) => { return; };
}
}
Upvotes: 0
Views: 189
Reputation: 161
Thanks to Clemens and Andy i have found the answer.
The Code Clemens provided works well and all. But the problem was that there are memory leaks when i was using the relative path.
I followed the instructions found on the following site: https://github.com/ButchersBoy/MaterialDesignInXamlToolkit/issues/746
This solved my issue.
Upvotes: 0
Reputation: 128013
There is a name mismatch between your static property and its changed event.
If the property is named titleFont
, the corresponding event must be named titleFontChanged
.
And of course there are widely accepted naming conventions, according to which property names should use Pascal casing, i.e. start with an uppercase letter.
So the property should perhaps better be named TitleFont
.
IMO, a better solution for the implementation of static property change notification would be to have a single StaticPropertyChanged
event for all static properties:
public static event PropertyChangedEventHandler StaticPropertyChanged;
public static FontFamily TitleFont
{
get { return _titleFont; }
set
{
_titleFont = value;
StaticPropertyChanged?.Invoke(null,
new PropertyChangedEventArgs(nameof(TitleFont)));
}
}
Upvotes: 2