绑定到嵌套静态类中的属性

Jan*_*m B 7 .net c# wpf binding

我有以下结构:

public static class Constants {
  public static class Foo {
    public static string Bar {
      get {
        //Constants.Foo.Bar == "FooBar"
        return "FooBar";
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我想将它绑定到usercontrol中的按钮.

<Button Content="{Binding Source={x:Static ns:Constants.Foo.Bar}}" />
Run Code Online (Sandbox Code Playgroud)

(其中ns指向定义"常量"的程序集和命名空间).
这导致两个错误:

  • "找不到类型'Constants.Foo'.请注意,类型名称区分大小写."
  • "未找到类型'ns:Constants.Foo'."

我也尝试过:

<Button Content="{Binding Source={x:Static ns:Constants+Foo.Bar}}" />
Run Code Online (Sandbox Code Playgroud)

这导致一个错误:

  • "未找到类型'ns:常量+ Foo'."

是否可以绑定到静态类中的静态类中的静态属性?如果有,怎么样?

bli*_*eis 13

这对我有用

 <Button Content="{Binding Source={x:Static local:Constants+Foo.Bar}}" />
Run Code Online (Sandbox Code Playgroud)

当地是

 xmlns:local="clr-namespace:WpfTestApp1"
Run Code Online (Sandbox Code Playgroud)