XAML:将文本框maxlength绑定到Class常量

Ash*_*Ash 19 xaml binding textbox constants maxlength

我试图将WPF文本框的Maxlength属性绑定到类中的已知常量.我正在使用c#.

该类的结构与以下内容差别不大:

namespace Blah
{
    public partial class One
    {
        public partial class Two
        {
             public string MyBindingValue { get; set; }

             public static class MetaData
             {
                 public static class Sizes
                 {
                     public const int Length1 = 10;
                     public const int Length2 = 20;
                 }
             }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

是的,它是深层嵌套的,但不幸的是在这种情况下,如果不需要大量重写,我就无法移动.

我希望我能够将文本框MaxLength绑定到Length1或Length2值,但我无法让它工作.

我期待绑定类似于以下内容:

<Textbox Text="{Binding Path=MyBindingValue}" MaxLength="{Binding Path=Blah.One.Two.MetaData.Sizes.Length1}" />
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏.

非常感谢

stu*_*ith 40

MaxLength="{x:Static local:One+Two+MetaData+Sizes.Length1}"
Run Code Online (Sandbox Code Playgroud)

期间参考属性.加号是指内部类别.


Ash*_*Ash 7

固定!

最初我尝试这样做:

{Binding Path=MetaData+Sizes.Length1}
Run Code Online (Sandbox Code Playgroud)

编译好了,但是绑定在运行时失败了,尽管类'Two'是路径无法解析为内部静态类的datacontext(我可以做类似的事情:{Binding Path = {x:Static MetaData + Size .Length1}}?)

我不得不稍微调整我的类的布局,但绑定现在正在工作.

新班级结构:

namespace Blah
{
    public static class One
    {
        // This metadata class is moved outside of class 'Two', but in this instance
        // this doesn't matter as it relates to class 'One' more specifically than class 'Two'
        public static class MetaData
        {
            public static class Sizes
            {
                public static int Length1 { get { return 10; } }
                public static int Length2 { get { return 20; } }
            }
        }

        public partial class Two
        {
            public string MyBindingValue { get; set; }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我的绑定声明如下:

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

MaxLength="{x:Static local:MetaData+Sizes.Length1}"
Run Code Online (Sandbox Code Playgroud)

这似乎工作正常.我不确定是否需要将常量转换为属性,但这样做似乎没有任何损害.

谢谢你们每一个人的帮助!