简体   繁体   中英

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 (FontStyle s ), 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.

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, eg:

FontStyleTreeItem = FontStyles.Italic;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM