XAML:绑定文本框最大长度为类常量 [英] XAML : Binding textbox maxlength to Class constant

查看:185
本文介绍了XAML:绑定文本框最大长度为类常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图一类中的一个文本框WPF的MaxLength属性绑定到一个已知常数深。我使用C#。

I am attempting to bind a WPF textbox's Maxlength property to a known constant deep within a class. I am using c#.

这个类有没有太多不同以下结构:

The class has a structure not too dissimilar to the following:

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;
                 }
             }
        }
    }
}

是的,它深深嵌套,但不幸的是在这种情况下,我不能动圆的东西非常多而无需重写巨大

Yes it is deeply nested, but unfortunately in this instance I can't move things round very much without huge rewrites required.

我希望我能够将文本框的maxlength绑定到长度1或长度2值,但我无法得到它的工作。

I was hoping I'd be able to bind the textbox MaxLength to the Length1 or Length2 values but I can't get it to work.

我期待的结合是类似以下内容:

I was expecting the binding to be something like the following:

<Textbox Text="{Binding Path=MyBindingValue}" MaxLength="{Binding Path=Blah.One.Two.MetaData.Sizes.Length1}" />

任何帮助是AP preciated。

Any help is appreciated.

非常感谢

推荐答案

固定!

起初,我试着这样做:

{Binding Path=MetaData+Sizes.Length1}

这编译OK,但是绑定在运行时,尽管失败的类两课正在路径无法解析成静态内部类(DataContext的可能我做类似:{绑定路径= {X:静态元数据+ Size.Length1}}?)

which compiled ok, however the binding failed at runtime, despite the Class 'Two' being the datacontext the path couldn't resolve into the inner static classes (could I have done something like : {Binding Path={x:Static MetaData+Size.Length1}} ?)

我有我的课一点点的布局摆弄,但结合现在的工作。

I had to fiddle with the layout of my classes a little but the binding is now working.

新的阶级结构:

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; }
        }
    }
}

然后我绑定声明如下:

Then my binding statement is as follows:

xmlns:local="clr-namespace:Blah"

MaxLength="{x:Static local:MetaData+Sizes.Length1}"

这似乎工作确定。我不知道是否不需要常量转换成性,但似乎并没有受到任何的伤害这样做的。

Which appears to work ok. I'm not sure whether or not the constants needed to be converted into properties, but there doesn't appear to be any harm in doing so.

三江源大家对你的帮助!

Thankyou everyone for your help!

这篇关于XAML:绑定文本框最大长度为类常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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