XAML:将文本框 maxlength 绑定到类常量 [英] XAML : Binding textbox maxlength to Class constant

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

问题描述

我试图将 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 绑定到 Length1 或 Length2 值,但我无法让它工作.

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}" />

感谢任何帮助.

非常感谢

推荐答案

已修复!

最初我尝试这样做:

{Binding Path=MetaData+Sizes.Length1}

编译正常,但是绑定在运行时失败,尽管二"类是数据上下文,但路径无法解析为内部静态类(我可以这样做:{Binding Path={x:Static元数据+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.

谢谢大家的帮助!

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

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