使用 {x:Static} 访问静态类中静态字段的属性

Gei*_*iry 0 c# wpf

在 WPF 中,我试图绑定到静态类中只读字段的属性,我已经看过这个,它将允许绑定到静态类中的 const 字符串,如下所示

<TextBlock Text="{x:Static A:MyConstants.SomeConstantString}" />
Run Code Online (Sandbox Code Playgroud)

我想做的是这样的

<TextBlock Text="{x:Static A:MyConstants.SomeReadOnlyField.StringProp}" />
Run Code Online (Sandbox Code Playgroud)

但这给了我一个错误

不支持嵌套类型

Cle*_*ens 6

假设这是class 中的SomeReadOnlyField一个字段,并且具有公共属性,您可以像这样编写您的 Binding:public static readonlyMyConstantsStringProp

Text="{Binding Path=StringProp
               Source={x:Static A:MyConstants.SomeReadOnlyField}}"
Run Code Online (Sandbox Code Playgroud)

请注意,虽然 BindingPath必须解析为公共属性的名称,但x:Static用于 Binding 的标记扩展Source可以很好地引用静态字段。