绑定到嵌套静态类中的属性 [英] Bind to property in a nested static class

查看:71
本文介绍了绑定到嵌套静态类中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的结构如下:

public static class Constants {
  public static class Foo {
    public static string Bar {
      get {
        //Constants.Foo.Bar == "FooBar"
        return "FooBar";
      }
    }
  }
}

我想将此绑定到用户控件中的按钮上.

I want to bind this to a button in a usercontrol.

<Button Content="{Binding Source={x:Static ns:Constants.Foo.Bar}}" />

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

(where ns points to the assembly and namespace where "Constants" is defined).
This results in two errors:

  • 找不到类型'Constants.Foo'.请注意,类型名称区分大小写."
  • 未找到类型'ns:Constants.Foo'."
  • "Cannot find the type 'Constants.Foo'. Note that type names are case sensitive."
  • "Type 'ns:Constants.Foo' was not found."

我也尝试过:

<Button Content="{Binding Source={x:Static ns:Constants+Foo.Bar}}" />

这会导致一个错误:

  • 未找到类型'ns:Constants + Foo'."
  • "Type 'ns:Constants+Foo' was not found."

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

Is is possible to bind to a static property in a static class in a static class? If yes, how?

推荐答案

这对我有用

 <Button Content="{Binding Source={x:Static local:Constants+Foo.Bar}}" />

本地为

 xmlns:local="clr-namespace:WpfTestApp1"

这篇关于绑定到嵌套静态类中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆