Remco
Remco

Reputation: 279

How to set the FontStyle of a TextBlock via Binding (MVVM)

My snipped code of the XAML:

<TextBlock Text="{Binding Name}" Foreground="{Binding FontColor}" FontStyle="{Binding FontStyleTreeItem}"/>

Snipped code Class TreeItem:

public System.Windows.FontStyles FontStyleTreeItem {get;set}

I want to assign the property "FontStyleTreeItem" something like:

treeItem.FontStyleTreeItem = System.Windows.FontStyles.Italic;

But this doesn't work because "System.Windows.FontStyles" is static. But I can't figure out how to give a good solution to set the above property.

I also tried to set the property as a FontStyle, so without the s at the end (FontStyles), but then the fontstyle of the textblock doesn't change.

public FontStyle FontStyleTreeItem { get { return FontStyle.Italic; } }

Can somebody see what I'm missing?

Already thanks.

Upvotes: 0

Views: 650

Answers (1)

mm8
mm8

Reputation: 169400

The type of the property should be System.Windows.FontStyle. It may still return a static value such as FontStyles.Italic:

public System.Windows.FontStyle FontStyleTreeItem { get { return System.Windows.FontStyles.Italic; } }

If you define the property like this:

public System.Windows.FontStyle FontStyleTreeItem { get; set; }

...you can set it to any FontStyle value, e.g.:

FontStyleTreeItem = FontStyles.Italic;

If you set it dynamically at runtime, you need to implement the INotifyPropertyChanged interface for the font style to change.

Upvotes: 1

Related Questions