简体   繁体   English

绑定FontFamily ViewModel的属性

[英]Binding FontFamily ViewModel's Property

I want to bind the FontFamily of a TextBox to a property I created on my ViewModel 我想将TextBox的FontFamily绑定到我在ViewModel上创建的属性

Here's my XAML : 这是我的XAML:

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

And here the property in my ViewModel : 这里是我的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. 加载视图时,属性的getter正在触发,但它不会在视图上传递。 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 : 我只是在处理错误的FontFamily对象..我用过:

System.Drawing.FontFamily

But the FontFamily must be of type : 但FontFamily必须是类型:

System.Windows.Media.FontFamily

Here's something I added to a ResourceDictionary intended to be a default style for my application: 这是我添加到ResourceDictionary旨在成为我的应用程序的默认样式:

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

When you want to use it, you set the FontFamily property like this: 当您想要使用它时,您可以像这样设置FontFamily属性:

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

Don't forget to put it in a ResourceDictionary or any Control Resource. 不要忘记将它放在ResourceDictionary或任何控制资源中。

Hope it helps; 希望能帮助到你;

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

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