在 XAML 个原语中绑定

Binding in XAML primitives

我正在开发 UWP 应用程序,并试图将一些 属性 绑定到 XAML 中的原语,但我找不到该怎么做。 我有一个带有静态值的 C# class:

 public class DevicePreferences
 {
     public static double InputTextFontSize = App.IsMobileDevice ? 22 : 18;
     public static double TitleTextBoxSize = App.IsMobileDevice ? 20 : 16;
     public static double ImageButtonSize = App.IsMobileDevice ? 40: 35;
 }

我看起来像这样:

<x:Double x:Key="MyCustomValue">{local:DevicePreferences.TitleTextBoxSize}</x:Double>

我想知道,有什么办法可以做到这一点吗?

如果您将要绑定的任何控件的 DataContext 设置为 TitleTextBoxSize 属性,您可以按照此处的建议直接绑定到它:https://social.msdn.microsoft.com/Forums/windowsapps/en-US/e0e426ba-4feb-4571-b80b-7ba8818079b6/uwpstatic-property-binding?forum=wpdevelop

但是你不能这样做:

<x:Double x:Key="MyCustomValue">{local:DevicePreferences.TitleTextBoxSize}</x:Double>

您只能在 <x:Double> 元素中放置常量 double 值。