Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding FontFamily ViewModel's Property

Tags:

c#

mvvm

wpf

xaml

I want to bind the FontFamily of a TextBox to a property I created on my ViewModel

Here's my XAML :

<TextBox Text="Test Font Binding" FontFamily="{Binding FontFirstContent}" />

And here the property in my ViewModel :

public FontFamily FontFirstContent
{
    get { return new FontFamily("Verdana"); }
}

When the view is loaded, the getter of the property is correctly firing but it's not passed on the view. All bindings on my view are working except this one, so i don't understand what's wrong with it ?

Edit : Okay it works fine ! I was just working on the wrong FontFamily object.. I've used :

System.Drawing.FontFamily

But the FontFamily must be of type :

System.Windows.Media.FontFamily
like image 633
Nidx Avatar asked Dec 11 '13 21:12

Nidx


1 Answers

Here's something I added to a ResourceDictionaryintended to be a default style for my application:

<FontFamily x:Key="MyFontFamily">Segoe UI</FontFamily>

When you want to use it, you set the FontFamily property like this:

<Label Content="Something" FontFamily="{DynamicResource MyFontFamily}"/>

Don't forget to put it in a ResourceDictionary or any Control Resource.

Hope it helps;

like image 139
Tico Avatar answered Nov 06 '22 12:11

Tico