简体   繁体   English

如何通过绑定(MVVM)设置TextBlock的FontStyle

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

My snipped code of the XAML: 我截断的XAML代码:

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

Snipped code Class TreeItem: 摘录的代码类TreeItem:

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

I want to assign the property "FontStyleTreeItem" something like: 我想分配属性“ FontStyleTreeItem”,例如:

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

But this doesn't work because "System.Windows.FontStyles" is static. 但这是行不通的,因为“ System.Windows.FontStyles”是静态的。 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. 我还尝试将属性设置为FontStyle,因此末尾没有s (FontStyle s ),但是文本块的fontstyle不变。

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 . 该属性的类型应为System.Windows.FontStyle It may still return a static value such as FontStyles.Italic : 它可能仍会返回静态值,例如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: ...您可以将其设置为任何FontStyle值,例如:

FontStyleTreeItem = FontStyles.Italic;

If you set it dynamically at runtime, you need to implement the INotifyPropertyChanged interface for the font style to change. 如果在运行时动态设置它,则需要实现INotifyPropertyChanged接口以更改字体样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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